array end issue

A

apropo

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

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

Régis Troadec

Hi,

apropo said:
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);
}

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


'\0' stands for the null terminating character in a string.
In fact, you're testing here if v 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
 
A

apropo

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);
}

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>v[j])
{
tmp=v;
v=v[j];
v[j]=tmp;
}
else
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(v>v[j])
{
tmp=v;
v=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 ?
 
G

Gordon Burditt

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
 
D

Derk Gwen

# int main(void)
# {
# int v[5];
# int i,j,tmp;
# for(i=0;i<5;i++)
# {
# printf("v[%d]:",i+1);
# scanf("%d",&v);
# }
#
# for(i=0;i<7;i++)
# if(v=='\0')
# {
# printf("END at %d",i);
# break;
# }
# getch();
# return 0;
# }
# EOF
# could anyone explain me ? is there a \0 at the end or not ?

Only if you read one in.
 
R

Régis Troadec

Hi again,

apropo said:
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);
}

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


You can't do that, sizeof(v) will return the size of a pointer on an int
(often 4 bytes).
A possible way to retrieve the size of your array is to add it as an
argument of sort_array().

/* a simple bubble sort */
int sort_array(int * my_array, int size_of_my_array)
{
int i, j;
for (i=size_of_my_array; i>0; i--)
{
for (j=2; j<=i; j++)
{
if (my_array[j-1] > my_array[j])
{
tmp=my_array[j-1]; my_array[j-1]=my_array[j]; my_array[j] =
tmp;
}
}
}
}

I'm surprised that you don't use size in your code below.
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>v[j])
{
tmp=v;
v=v[j];
v[j]=tmp;
}
else
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(v>v[j])
{
tmp=v;
v=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 ?
 
C

CBFalconer

Gordon said:
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..

Very few soles are immortal, be they fish or footwear :)
 
S

Sean Kenwrick

apropo said:
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);

file://-----------------------------------
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);

You are printing out values that have not yet been assigned and becuase you
are using i+1 you will read past the end of your array.....

scanf("%d",&v);
}

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

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>v[j])
{
tmp=v;
v=v[j];
v[j]=tmp;
}
else
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(v>v[j])
{
tmp=v;
v=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 ?
 
T

those who know me have no need of my name

in comp.lang.c i read:
for(i=0;i<10;i++)
{
printf("v[%d]:",i+1);

You are printing out values that have not yet been assigned and becuase you
are using i+1 you will read past the end of your array.....

scanf("%d",&v);


it's fine, it's merely text. it is misleading (to other programmers) to
ask for v[1] while storing v[0], but there's nothing wrong with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,140
Messages
2,570,810
Members
47,357
Latest member
sitele8746

Latest Threads

Top