Tag: const pointer

使用malloc()为const char字符串动态分配内存

我正在编写一个从.ini文件中读取值的程序,然后将该值传递给接受PCSTR的函数(即const char *)。 该函数是getaddrinfo() 。 所以,我想写PCSTR ReadFromIni() 。 要返回一个常量字符串,我计划使用malloc()分配内存并将内存转换为常量字符串。 我将能够获得从.ini文件中读取的确切字符数。 这种技术还好吗? 我真的不知道还能做什么。 以下示例在Visual Studio 2013中正常运行,并根据需要打印出“hello”。 const char * m() { char * c = (char *)malloc(6 * sizeof(char)); c = “hello”; return (const char *)c; } int main(int argc, char * argv[]) { const char * d = m(); std::cout << d; // use PCSTR […]

c – 这个2 const意味着什么?

码: const char * const key; 上面的指针有2个const,我第一次看到这样的东西。 我知道第一个const使指针指向的值不可变,但第二个const是否使指针本身不可变? 有人可以帮忙解释一下吗? @Update: 我写了一个程序,certificate答案是正确的。 #include void testNoConstPoiner() { int i = 10; int *pi = &i; (*pi)++; printf(“%d\n”, i); } void testPreConstPoinerChangePointedValue() { int i = 10; const int *pi = &i; // this line will compile error // (*pi)++; printf(“%d\n”, *pi); } void testPreConstPoinerChangePointer() { int i […]