如何获取指向数组的指针的大小。 [英] How to take the size of a pointer to array.

查看:206
本文介绍了如何获取指向数组的指针的大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下代码


包括< stdio.h>

#include< stdlib.h>


int main(无效){


char * msg_list [] = {" apple,"橙色,葡萄" };


printf(" name:%s \ n",msg_list [0]);

printf(" size:%d \\ \\ n",sizeof(msg_list));


返回0;

}


我以为拿尺码(msg_list)会给我字符串的大小

" apple" (即6)。相反,我得到了以下的



名称:苹果

尺寸:12


我在这里缺少什么?


乍得

Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I''m missing here?

Chad

推荐答案

乍得说:
Chad said:

给出以下代码


包含< stdio.h>

# include< stdlib.h>


int main(void){


char * msg_list [] = {" apple,"橙色,葡萄" };


printf(" name:%s \ n",msg_list [0]);

printf(" size:%d \\ \\ n",sizeof(msg_list));


返回0;

}


我以为拿尺码(msg_list)会给我字符串的大小

" apple" (即6)。相反,我得到了以下的



名称:苹果

尺寸:12


我在这里缺少什么?
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I''m missing here?



msg_list是一个由三个指向char的数组。在你的平台上,它出现
,每个指向char的指针占用四个字节。因此,

数组的大小是3 * 4 = 12.在其他系统上,你可能得到不同的结果。

(例如,在MS-DOS下,你可能会很好获得3 * 2 = 6,而在一些

DSP上你可能得到3 * 1 = 3.)


sizeof msg_list [0]会给你一个字符的大小*。


sizeof" apple",但是,无论平台的价值是多少,你都会得到6个你想要的东西。< br $>

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




Richard Heathfield写道:

Richard Heathfield wrote:

Chad说:
Chad said:

给出以下代码


包括< stdio.h>

#include< stdlib.h>


int main(void){


char * msg_list [] = {" apple,"橙色,葡萄" };


printf(" name:%s \ n",msg_list [0]);

printf(" size:%d \\ \\ n",sizeof(msg_list));


返回0;

}


我以为拿尺码(msg_list)会给我字符串的大小

" apple" (即6)。相反,我得到了以下的



名称:苹果

尺寸:12


我在这里缺少什么?
Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I''m missing here?



msg_list是一个由三个指向char的数组。在你的平台上,它出现
,每个指向char的指针占用四个字节。因此,

数组的大小是3 * 4 = 12.在其他系统上,你可能得到不同的结果。

(例如,在MS-DOS下,你可能会很好获得3 * 2 = 6,而在一些

DSP上你可能得到3 * 1 = 3.)


sizeof msg_list [0]会给你一个字符的大小*。


sizeof" apple",但是,无论平台的价值是多少,你都会得到6个你想要的东西。


msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.



无论如何都要从这种结构中获得苹果的大小而不用

必须输入

sizeof" apple"

Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"


Chad写道:
Chad wrote:

Richard Heathfield写道:
Richard Heathfield wrote:

>> Chad说:

>>Chad said:


>>>给出以下代码

包括< stdio.h>
#include< stdlib.h>

int main(无效){

ch ar * msg_list [] = {" apple,"橙色,葡萄" printf(" name:%s \ n",msg_list [0]);
printf(" size:%d \ n",sizeof(msg_list) );

返回0;
}
我认为拿sizeof(msg_list)会给我字符串的大小
" apple" (即6)。相反,我得到以下内容:

名称:苹果
大小:12

我在这里缺少什么?
>>>Given the following code

include <stdio.h>
#include <stdlib.h>

int main(void) {

char *msg_list[] = {" apple", " orange", " grape" };

printf("name: %s \n", msg_list[0]);
printf("size: %d \n", sizeof(msg_list));

return 0;
}

I thought taking sizeof(msg_list) would give me the size of the string
"apple" (ie 6). Instead, I get
the following:

name: apple
size: 12

What I''m missing here?


msg_list是一个由三个指向char的数组。在你的平台上,它出现,每个指向char的指针占用四个字节。因此,
数组的大小为3 * 4 = 12.在其他系统上,您可能会得到不同的结果。
(例如,在MS-DOS下,您可能会得到3 * 2 = 6,而在某些
DSP上你可能得到3 * 1 = 3.)

sizeof msg_list [0]会给你一个char *的大小。

sizeof"然而,无论平台是什么,苹果都会给你6个你期望得到的东西。


msg_list is an array of three pointers-to-char. On your platform, it
appears, each pointer-to-char occupies four bytes. Hence the size of the
array is 3 * 4 = 12. On other systems, you might get different results.
(For example, under MS-DOS you might well get 3 * 2 = 6, whereas on some
DSPs you might get 3 * 1 = 3.)

sizeof msg_list[0] would give you the size of a char *.

sizeof "apple", however, will give you the 6 you expected to get, regardless
of the platform.




无论如何都要获得这种结构的苹果大小没有

必须输入

sizeof" apple"



Is there anyway to get the size of apple from this construction without
having to type
sizeof "apple"



strlen( "苹果"); ?????????????????

strlen("apple"); ??????????????????????????


这篇关于如何获取指向数组的指针的大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆