阵列结束问题 [英] array end issue

查看:69
本文介绍了阵列结束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(void)

{

int v [5];

int i,j,tmp;

for(i = 0; i< 5; i ++)

{

printf(" v [%d]:",i + 1) ;

scanf("%d"& v [i]);

}


for(i = 0; i< 7; i ++)

if(v [i] ==''\''')

{

printf (END at%d,i);

休息;

}

getch();

返回0;

}

EOF

有人可以解释一下吗?最后是否有\0?

解决方案




apropo < AB ******* @ netscape.net> écritdansle message de news:

em **************** @ news.chello.at ...

int main(void)
{
int v [5];


也许你的意思是char v [5]; ?

int i,j,tmp;
for(i = 0; i< 5; i ++)
{
printf(" v [%d] :",i + 1);
scanf("%d"& v [i]);
}

for(i = 0; i< 7; i ++)
if(v [i] ==''\''')


''\ 0''代表空终止字符一个字符串。

事实上,如果v [i]为零,你在这里测试。


另一件事是你是访问v阵列外部,

因为v只包含5个元素...

{
printf(" END at%d,i);
break;
}
getch();


getch()是非标准C.

返回0;
}
EOF
任何人都可以解释我?最后是否有\0?




HTH

Regis


我实际上陷入了该死的功能。我无法在另一个函数中获得数组

的大小。从现在起我就有了这样的事情:

#include< stdio.h>

#include< stdlib.h>

int sort_array( int * v,int type);


// ---------------------------- -------

int main(无效)

{

int v [10];

int * s;

s = v;

int i,j,tmp;

for(i = 0; i< 10; i ++ )

{

printf(" v [%d]:",i + 1);

scanf("%d" ;,& v [i]);

}


printf(" SIZE:%d",sort_array(s,1));

getch();

getch();

返回0;

}

// -----------------------------------


int sort_array(int * v,int type)//在main()中我会做...

{

int size = sizeof(v); // size = sizeof(v)/ sizeof(v [0])得到大小,但是

int i,j,tmp; //这里我们只有v [0]地址,指针

if(type)//到数组....换句话说 - 不知道如何实现它

for(i = 0; i< 5; i ++)

for(j = i; j< 5; j ++)

if(v [i] > v [j])

{

tmp = v [i];

v [i] = v [j];

v [j] = tmp;

}

else

for(i = 0; i< 5; i ++)

for(j = 0; j <5; j ++)

if(v [i]> v [j])

{

tmp = v [i];

v [i] = v [j];

v [j] = tmp;

}

返回尺寸;

}

EOF

这不是预期的工作!我根本不知道如何实现我的

little和imperfect sort()函数。你有什么想法吗?


>我实际上陷入了该死的功能。


当调用愤怒

未定义的行为时,非该死的功能有点恶意,并且他们通常会削减鞋类费用,因为该死的功能通常会带来你的不朽鞋底..

我无法在另一个函数中获得数组的大小




如果你通过了数组作为函数参数,它作为

指针传递给函数的第一个元素。你不能使用该指针获得数组的

大小。传递数组的大小

(sizeof(s)),或者元素数量(sizeof(s)/ sizeof(s [0])

),或类似于另一个论点的东西。如果调用者通过

a谎称元素的大小或数量,那么编译器将报b / b
报复。


你可能会注意到qsort()接受一个指向

数组(转换为void *)的指针,一些元素,每个元素的大小,

和a比较函数指针。如果有可能获得数组的

大小,为什么需要传递的元素数量?


Gordon L. Burditt


int main(void)
{
int v[5];
int i,j,tmp;
for(i=0;i<5;i++)
{
printf("v[%d]:",i+1);
scanf("%d",&v[i]);
}

for(i=0;i<7;i++)
if(v[i]==''\0'')
{
printf("END at %d",i);
break;
}
getch();
return 0;
}
EOF
could anyone explain me ? is there a \0 at the end or not ?

解决方案

Hi,

"apropo" <ab*******@netscape.net> a écrit dans le message de news:
em****************@news.chello.at...

int main(void)
{
int v[5];
Perhaps would you mean char v[5]; ?
int i,j,tmp;
for(i=0;i<5;i++)
{
printf("v[%d]:",i+1);
scanf("%d",&v[i]);
}

for(i=0;i<7;i++)
if(v[i]==''\0'')
''\0'' stands for the null terminating character in a string.
In fact, you''re testing here if v[i] is zero or not.

Another thing is that you''re accessing outside your v array,
since v contains only 5 elements...
{
printf("END at %d",i);
break;
}
getch();
getch() is non-standard C.
return 0;
}
EOF
could anyone explain me ? is there a \0 at the end or not ?



HTH
Regis


i''m actually stuck in a damn function. I cannot get the size of array
inside another function. Since now i have somethin like:
#include <stdio.h>
#include <stdlib.h>
int sort_array(int *v,int type);

//-----------------------------------
int main(void)
{
int v[10];
int *s;
s=v;
int i,j,tmp;
for(i=0;i<10;i++)
{
printf("v[%d]:",i+1);
scanf("%d",&v[i]);
}

printf("SIZE:%d",sort_array(s,1));
getch();
getch();
return 0;
}
//-----------------------------------

int sort_array(int *v,int type) // in main() i would do ...
{
int size=sizeof(v); // size=sizeof(v)/sizeof(v[0]) to get the size, but
int i,j,tmp; // here we have just the v[0] address, the pointer
if(type)//to the array....with other words - no clue how to implement it
for(i=0;i<5;i++)
for(j=i;j<5;j++)
if(v[i]>v[j])
{
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
else
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(v[i]>v[j])
{
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
return size;
}
EOF
THIS IS NOT EXPECTING TO WORK! I just got no idea how to implement my
little and imperfect sort() function. Do you have any idea ?


>i''m actually stuck in a damn function.

non-damn functions are a bit less malicious when invoking the wrath
of undefined behavior, and they generally cut down on footwear
expense, as the damn functions generally take your immortal sole..

I cannot get the size of array
inside another function.



If you pass an array as a function argument, it is passed as a
pointer to the first element of the function. You CANNOT get the
size of the array using that pointer. Pass the size of the array
(sizeof(s)), or or the number of elements ( sizeof(s)/sizeof(s[0])
), or something similar as another argument. If the caller passes
a lie for the size or number of elements, then the compiler will
get its revenge.

You might notice that qsort() accepts as arguments a pointer to an
array (cast to void *), a number of elements, the size of each element,
and a comparison function pointer. If it were possible to get the
size of the array, why would it need the number of elements passed?

Gordon L. Burditt


这篇关于阵列结束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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