473,487 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array end issue

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 ?

Nov 14 '05 #1
10 1263
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
Nov 14 '05 #2
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 ?

Nov 14 '05 #3
>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
Nov 14 '05 #4
apropo <ab*******@netscape.net> wrote:
# 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 ?

Only if you read one in.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.
Nov 14 '05 #5
Hi again,

"apropo" <ab*******@netscape.net> a écrit dans le message de news:
wj****************@news.chello.at...
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
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[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 ?

Nov 14 '05 #6
Gordon Burditt wrote:
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..


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7
in comp.lang.c i read:
int v[5]; for(i=0;i<7;i++)
if(v[i]=='\0')


you are using elements beyond the bounds of the array -- anything can
happen as a result. don't do this.

--
a signature
Nov 14 '05 #8

"apropo" <ab*******@netscape.net> wrote in message
news:wj****************@news.chello.at...
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[i]);
}

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[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 ?

Nov 14 '05 #9
CBFalconer wrote:
Gordon Burditt wrote:
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..

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


A typo. He obviously meant immoral. ;)

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.
Nov 14 '05 #10
in comp.lang.c i read:
"apropo" <ab*******@netscape.net> wrote in message
news:wj****************@news.chello.at...

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[i]);


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.

--
a signature
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2596
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
29
5411
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
4
2802
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
14
4042
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
272
13865
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.