English 中文(简体)
果园和果园之间的差异
原标题:Difference between char *str and char str[]
  • 时间:2013-07-05 08:26:29
  •  标签:
  • c

我将同样的直观价值分配给点人和<条码>。 阵容

char *str = "hello" "world";  
char str1[] = "hello" "world";

然后使用<代码>sizeof()

sizeof(str);     //  on my computer, it s 8 !!
sizeof(str1);    //  return 11, which is right

但两者均可通过<>%予以印刷:

printf("%s
%s
", str, str1);

因此,sizeof(str);return a wrong Value?

最佳回答

因此,大小为何?

这并不是因为<条码>>>通过斜体表示其歌剧的大小。 页: 1 C99标准(第869条草案)第2条:

www.un.org/spanish/ecosoc <sizeof营运人产生其歌剧体(以斜体表示)的大小,可能是一种表达方式或一种类型的母体名称。 大小由歌剧类型决定。 结果是愤怒的。 如果歌剧的类型是不同的系列,则对歌剧进行评价;否则,对歌剧不作评价,结果是惯性的。

Therefore:

sizeof(str)  == sizeof(char*)
sizeof(str1) == sizeof(char[11])
问题回答

The first one is a pointer. You don t count the number of chars but the size of the pointer in memory. You need to understand the difference. char* point to a position in memory that you assume contains chars in a row. char[x] IS a position in memory which has x chars on a row there.

价值8是指点员的大小,因为梯体实际上是一个指点,即,你可以将其重新定位到另一个直面。

The variable str1 is an array of characters and as such cannot be reassigned and its size is related to the size of the array.

First one is pointer to char, other one is array. Pointer points to one char only, size is one byte.

你可以将果园用作阵列,但你们需要知道,可以分开。

http://www.un.org/Depts/DGACM/index_spanish.htm 因此,您的<代码>sizeof()将退回不同的数值,并交回类型大小,而不是座标的长度。

您应使用<代码>strlen(),以检查地体大小。 铭记用<代码>strlen(>对每一条进行检测时,必须终止()。

对于常数(使用“普通”法的计算法),汇编者添加了终止无效的内容。

对C方案拟订语言本身所载示意图的唯一支持是,汇编者将把引证的常态转化为无名的扼杀,储存在静态记忆中。

https://en.wikipedia.org/wiki/C_string_handling

char *ss = "hello";
char sa[6] = "hello";
char sc[6] = { h , e , l , l , o ,0x0};
printf("mem: %p  ",ss);
printf("mem: %p  ",sa);
printf("mem: %p  ",sc);

printf("size: %d  ",sizeof(ss));
printf("size: %d  ",sizeof(sa));
printf("size: %d  ",sizeof(sc));

printf("size: %x  ",sizeof(&ss));
printf("size: %x  ",sizeof(&sa));
printf("size: %x  ",sizeof(&sc));

printf("ptr: %d  ",sizeof(void*));
printf("chr: %d  ",sizeof(char));
printf("int: %d  ",sizeof(int));
printf("
");

//This will show some of the reason 
//that the sizeof operator returns 
//unexpected results.
//Note that the sizeof operator
//returns the size in bytes




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签