sizeof和传递指针 [英] sizeof and passing pointer

查看:89
本文介绍了sizeof和传递指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是一个C程序员,我有一个关于数组的问题和

查找数组中存在的条目数。


如果我将一个结构数组传递给一个函数,那突然间我就不能使用

sizeof(array)/ sizeof(array [0] )在该功能中?


帮助 - 我错过了什么点?


如下图所示。注释掉的代码在哪里作为

完全相同的代码,只包含在一个函数中不起作用?


谢谢...


#include< stdio.h>


typedef struct {

char * name;

char * cat;

char * desc;

int wday;

int dur;

} entry_t ;


entry_t entries [] = {

{" dummy"," todo"," explanation",1,20},

{" not"," bla"," foobar",1,10},

{" check"," nowhere",for something something ;,1,20}

};


void task_dump(entry_t *);


int main( int argc,char * argv []){

/ *

int i = 0;

int max = 0;


max = sizeof(entries)/ sizeof(entries [0]);

for(i = 0; i< max; i ++){

printf(" - %02d \ n",i);

printf(&nam) e:%s \ n",entries [i] .name);

printf(" catagory:%s \ n",entries [i] .cat);

printf(" descript:%s \ n",entries [i] .desc);

printf(" weekday:%d \ n",entries [i] .wday);

printf(" duration:%d \ n",entries [i] .dur);

}

* /


task_dump(条目);


返回0;

}


void task_dump(entry_t * data){

int i = 0;

int max = 0;


/ *这不起作用? * /

max = sizeof(data)/ sizeof(data [0]);

for(i = 0; i< max; i ++){

printf(" - %02d \ n",i);

printf(" name:%s \ n",data [i] .name);

printf(" catagory:%s \ n",data [i] .cat);

printf(" descript:%s \ n", data [i] .desc);

printf(" weekday:%d \ n",data [i] .wday);

printf(" duration :%d \ n",data [i] .dur);

}


返回;

}

Hello,

I''m a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.

If I pass an array of structures to a function, then suddenly I can''t use
sizeof(array) / sizeof(array[0]) anymore within that function ?

Help - What point am I missing ?

To show an example below. The commented out code that works where as the
exact same code, only wrapped in a function does not work ?

Thank you...

#include <stdio.h>

typedef struct {
char *name;
char *cat;
char *desc;
int wday;
int dur;
} entry_t;

entry_t entries[] = {
{ "dummy", "todo", "explanation", 1, 20 },
{ "not", "bla", "foobar", 1, 10 },
{ "check", "nowhere", "for something", 1, 20 }
};

void task_dump(entry_t *);

int main(int argc, char *argv[]) {
/*
int i = 0;
int max = 0;

max = sizeof(entries) / sizeof(entries[0]);
for(i = 0; i < max; i++) {
printf(" -- %02d\n", i);
printf(" name : %s\n", entries[i].name);
printf(" catagory : %s\n", entries[i].cat);
printf(" descript : %s\n", entries[i].desc);
printf(" weekday : %d\n", entries[i].wday);
printf(" duration : %d\n", entries[i].dur);
}
*/

task_dump(entries);

return 0;
}

void task_dump(entry_t *data) {
int i = 0;
int max = 0;

/* this doesn''t work ? */
max = sizeof(data) / sizeof(data[0]);
for(i = 0; i < max; i++) {
printf(" -- %02d\n", i);
printf(" name : %s\n", data[i].name);
printf(" catagory : %s\n", data[i].cat);
printf(" descript : %s\n", data[i].desc);
printf(" weekday : %d\n", data[i].wday);
printf(" duration : %d\n", data[i].dur);
}

return;
}

推荐答案



" jason" < ji *** @ notmal.comwrote in message

"jason" <ji***@notmal.comwrote in message

你好,


我要开始C程序员和我有一个关于数组的问题和

查找数组中存在的条目数。
Hello,

I''m a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.



数组在传递给它时会衰减为指针功能。所以你需要

将元素数作为一个单独的参数传递。


当你开始编写真正的程序时,你会发现案例的数量

你知道数组在编译时的大小很少。通常

大小由用户输入的数据决定,因此您必须使用malloc()分配

空间。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

Arrays decay to pointers when you pass them to functions. So you need to
pass in the number of elements as a separate parameter.

When you start writing real programs you will find that the number of cases
where you know an array''s size at compile time is quite few. Usually the
size is determined by the data the user inputs, so you must allocate the
space with malloc().

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


>我要开始了C程序员和我有关于数组的问题和
>I''m a beginning C programmer and I have a question regarding arrays and

>查找数组中存在的条目数。

如果我通过了一个函数的结构数组,然后突然我不能在该函数中使用
sizeof(array)/ sizeof(array [0])?
>finding the number of entries present within an array.

If I pass an array of structures to a function, then suddenly I can''t use
sizeof(array) / sizeof(array[0]) anymore within that function ?



你可以将一个*指针*传递给一个函数的结构数组。

它可能看起来像你正在通过数组,语法使得它很容易被愚弄,但你不是。你传递的数组的大小

并没有传递指向数组的指针,所以如果你想要大小,

你必须手动完成(例如将它作为另一个参数传递给

函数。

You can pass a *pointer* to an array of structures to a function.
It may look like you''re passing the array, and the syntax makes it
easy to be fooled, but you''re not. The size of the array you passed
isn''t passed with the pointer to the array, so if you want the size,
you have to do it manually (e.g. pass it as another argument to the
function).


>帮助 - 我错过了什么点?
>Help - What point am I missing ?



应用于指针的sizeof不会给出它指向

at的大小,除非偶然发生意外。它给出了指针的大小。

sizeof applied to a pointer does not give the size of what it points
at, except occasionally by accident. It gives the size of the pointer.


On Sun,2007年10月21日18:36:09 +0100,Malcolm McLean写道:
On Sun, 21 Oct 2007 18:36:09 +0100, Malcolm McLean wrote:

" jason" < ji *** @ notmal.com写了留言
"jason" <ji***@notmal.comwrote in message

>你好,

我是一个C程序员,我有关于数组的问题和
查找数组中存在的条目数。
>Hello,

I''m a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.



当你将它们传递给函数时,数组会衰减到指针。所以你需要

传递元素的数量作为一个单独的参数。


当你开始编写真正的程序时,你会发现
你知道数组在编译时的大小很少的情况。

通常大小由用户输入的数据决定,所以你必须

使用malloc()分配空间。

Arrays decay to pointers when you pass them to functions. So you need to
pass in the number of elements as a separate parameter.

When you start writing real programs you will find that the number of
cases where you know an array''s size at compile time is quite few.
Usually the size is determined by the data the user inputs, so you must
allocate the space with malloc().



好​​的谢谢你!


但是要确保;在这种情况下,衰变究竟意味着什么?并且

什么属性的指针,当传递给函数时实际上

改变了?


谢谢。


杰森。

Ok thankx for that !

But just to make sure; what does `decay'' exactly mean in this case ? And
what properties of the pointer, when passed to a function actually
change ?

Thank you.

Jason.


这篇关于sizeof和传递指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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