gcc 4.4.4 c89
null终止字符串的标准方法是什么?当我使用时,NULL我收到一条警告信息.
*dest++ = 0;
*dest++ = '\0';
*dest++ = NULL; /* Warning: Assignment takes integer from pointer without a cast */
Run Code Online (Sandbox Code Playgroud)
源代码:
size_t s_strscpy(char *dest, const char *src, const size_t len)
{
/* Copy the contents from src to dest */
size_t i = 0;
for(i = 0; i < len; i++)
*dest++ = *src++;
/* Null terminate dest */
*dest++ = 0;
return i;
}
Run Code Online (Sandbox Code Playgroud)
另一个问题:我故意注释掉null终止的那一行.但是,它仍然正确地打印出dest的内容.此函数的调用者将通过包括NULL或不包括字符串的长度发送.即strlen(src) + 1或stlen(src). …
我想知道是否有一种方法可以char*指向char数组的内容,以便可以修改char*跨函数。
例如
void toup(char* c) {
char array[sizeof(c)];
for (int x;x<strlen(c);x++){
array[x]=toupper(c[x]);
}
}
int main(){
char *c="Hello";
toup(c);
}
Run Code Online (Sandbox Code Playgroud)
试图使之array = char*似乎不起作用。是否可以使char *指向char数组?
我有一个像这样的字符串:
char* hello = "Hello, world!";
Run Code Online (Sandbox Code Playgroud)
我必须循环遍历此字符串中的每个字符.我试过这些,但他们要么给我编译错误,要么访问违规,或者只是永远不要离开循环:
for( char* p = hello; p!=0; p++ ) printf("%x\n", p);
for( char* p = &hello; p!=0; p++ ) printf("%x\n", p);
for( char* p = *hello; p!=0; p++ ) printf("%x\n", p);
for( char* p = hello; *p!=0; *p++ ) printf("%x\n", *p);
Run Code Online (Sandbox Code Playgroud)
我真的不明白指针在C中是如何工作的,我只是随机地放置星号直到它起作用,在这种情况下它不会.
我没有strlen像其他问题一样使用.