#include <stdio.h>
#define N 10
char str2[N]={"Hello"};
int main(){
printf("sizeof(str2): %d bytes\n", sizeof(str2));
printf("sizeof(&str2): %d bytes\n", sizeof(&str2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof(str2): 10 bytes
sizeof(&str2): 4 bytes
Run Code Online (Sandbox Code Playgroud)
我知道str2单独是数组中第一个元素的地址str2.并且当str2它的参数何时sizeof返回整个数组str2的大小.
另外,&str2也是arr中第一个元素的地址,str2但是来自不同的类型(char (*)[N]==指向数组的指针).但是&str2当它是一个论证时,它是如何表现的sizeof?
我试图理解为什么下面的代码片段给出了分段错误:
void tokenize(char* line)
{
char* cmd = strtok(line," ");
while (cmd != NULL)
{
printf ("%s\n",cmd);
cmd = strtok(NULL, " ");
}
}
int main(void)
{
tokenize("this is a test");
}
Run Code Online (Sandbox Code Playgroud)
我知道strtok()实际上并没有对字符串文字进行标记,但在这种情况下,line直接指向"this is a test"内部为数组的字符串char.是否有任何令牌化line而不将其复制到数组中?
我是C的新手,我正在尝试将日期/时间字符串拆分为单独的变量.但是,当我逐行遍历gdb中的代码时,它可以工作,但是,当我让它正常运行而没有断点时它会出错并且我看不清楚原因.
以下是代码:
char * dateTimeString = "2011/04/16 00:00";
char dateVar[11];
char timeVar[6];
if (splitTimeAndDateString(dateVar, timeVar, dateTimeString))
{
exit(1);
}
printf("Date: %s\tTime: %s\n", dateVar, timeVar);
Run Code Online (Sandbox Code Playgroud)
以下是功能
int splitTimeAndDateString(char date[11], char time[6], char * dateString)
{
char *token;
token = strtok(dateString, " ");
int i = 0;
while (token != NULL)
{
if (i == 0)
{
strcpy(date, token);
}
else if (i == 1)
{
strcpy(time, token);
}
else
{
printf("Overrun date time string\n");
return 1;
}
token = strtok(NULL, " …Run Code Online (Sandbox Code Playgroud)