View Full Version : passing arays to functions


system_eyes
#include <stdio.h>
#define size 10
void modifyarr (int[],int)

int main(){
int c[size] = {0};
modifyarr(c,size);
printf("---------------------------");
for(i=0,i!=size,i++)
printf("%d\n",c[i]);
return 0 }

void modifyarr (int b[],int size)
{
for(i=0,i!=size,i++)
b[i]=i;
printf("%d\n",n[i]);
}

well, what about the result, would it be
0
1
2
3
4
5
6
7
8
9
------------------
0
1
2
3
4
5
6
7
8
9







or it would be like this
0
1
2
3
4
5
6
7
8
9
-------------------
0
0
0
0
0
0
0
0
0
0

actually i am not joking, but answering this question for me will
let me understand some important thing,
[call by value and call by refrence],
i wana know if midifyarr will modify the original
array , in this case the answer would be the first result.
or it will not modify it [result 2].

thanks a lot

uniball
Oh well, It'll :-)
here c holds the address of the 1st member "pointer to the 1st member".
so when we pass the array to the function modifyarr() we are passing the
address of memory of the 1st element
here is a call be reference.
it'll be:
0
1
2
3
4
5
6
7
8
9
-------------------
0
0
0
0
0
0
0
0
0
0

angoranimi
no such thing as call by reference in C. you do the same trick by playing with pointers (in which case, you are passing pointers BY VALUE, yet, they point to the same data).

so, in C++, this would work:
void modify_int (int &x) { x++; }
main() { int y=2; modify_int (y); printf ("%i", y); }

Wouldn't work with C, but you can do a:
void modify_int (int *x) { *x++; }
main() { int y=2; modify_int (&y); printf ("%i", y); }

MaherG
Please use the code tags

[ CODE ]
.
.
.
[ /CODE ]

Without the spaces between the CODE and []

uniball
i'm sure i know the concept, though the i've missed the right words
btw: system_eyes code won't compile ;)

anyone need the modified code ?

system_eyes
Originally posted by uniball
i'm sure i know the concept, though the i've missed the right words
btw: system_eyes code won't compile ;)

anyone need the modified code ?
well, i wonder why it will not be compiled ,?
and if it wrong plz correct me, yeah i wana the right code;)

sattia
uniball
execuse me but seems u were tricked somehow the code above when corrected should display the first case with the two sections are from 0 to 9.

systenm_eyes
ur code is full of tooooo many syntax errors. correct it first.
also I could guess frrom the way u do program that ur still not understanding C in an apropriate way.
For example, u type

int c[size]={0};

You thought this way the array would fill all its elements automatically with 0. If so; then it is wrong because this way u tell the compiler that the array has the first element for sure a 0 while others r left untouched they could be zero or somehting else according to where they will get located in memory.
Second u dnt follow the convention when u use constant. The convention states that constants has to be all CAPITALS. ur constant size should be used as SIZE. This was any one else reading ur code would determine quickly it is a constant.
Third learn to follow a standard of coding style. This way ur code is considered professional. You can read much more about coding standards on http://www.cs.uiowa.edu/~jones/syssoft/style.html
Execuse me for my points but ur still young and if u want to learn something in computers world then u should learn it the right way. You may feel it hard first but when u get used to it will be so easy and others would benefit by ur codes easily.

uniball
#include <stdio.h>
#define SIZE 10
void modifyarr (int[], int);

int
main ()
{
int i;
int c[SIZE] = { 0 };
modifyarr (c, SIZE);
printf ("---------------------------\n");
for (i = 0; i != SIZE; i++)
{
printf ("%d\n", c[i]);
}
return 0;
}

void
modifyarr (int b[], int size)
{
int i;
for (i = 0; i != size; i++)
{
printf ("%d\n", b[i]);
b[i] = i;
}
}

uniball
Originally posted by sattia
uniball
execuse me but seems u were tricked somehow the code above when corrected should display the first case with the two sections are from 0 to 9.

i think it depends, You can initialise it to zeros ;)
or maybe it'll display garbage!
actually i didn't have a look at the whole code, I had a look only at the arguments given to modifyarr() and how it process them.
I'm really in a very bad situation - in my life actually - Execuse me!

system_eyes
Originally posted by sattia
uniball
execuse me but seems u were tricked somehow the code above when corrected should display the first case with the two sections are from 0 to 9.

systenm_eyes
ur code is full of tooooo many syntax errors. correct it first.
also I could guess frrom the way u do program that ur still not understanding C in an apropriate way.
For example, u type

int c[size]={0};

You thought this way the array would fill all its elements automatically with 0. If so; then it is wrong because this way u tell the compiler that the array has the first element for sure a 0 while others r left untouched they could be zero or somehting.
thanks man, ibut, int c[SIZE] = {0} will assign the whole elements to zero, and i am sure of this .

conecrning the symbolic constant, yeah , i guess u are right

sattia
run this code and tell me the result

main()
{
int x[10] = {100};
int i;

for(i = 0; i < 10; i++)
printf("x[%d] = %d\n", i, x[i]);
}

if u were rite then u would get

x[0] = 100
x[1] = 100
.
.
x[9] = 100

but unfortunately ur not so u would get

x[0] = 100
x[1] = 0 or anything else depends on memory
.
.
x[9] = 0 or anything else depends on memory

OneOfOne
system_eyes: just a tip, if you want to know if something will work or not you could just try it..
the best way to learn is to actually make mistakes and learn from it (unless your a doctor or making fs drivers :p).

peace

system_eyes
Originally posted by sattia
run this code and tell me the result

main()
{
int x[10] = {100};
int i;

for(i = 0; i < 10; i++)
printf("x[%d] = %d\n", i, x[i]);
}

if u were rite then u would get

x[0] = 100
x[1] = 100
.
.
x[9] = 100

but unfortunately ur not so u would get

x[0] = 100
x[1] = 0 or anything else depends on memory
.
.
x[9] = 0 or anything else depends on memory

i am still right , here is the idea man,
well, suppose u declare an array of 100 as in ur programm,
and u assign an value of 100 to the first elemnt x[0],
now the compiler will automatically assin the other elements to zero [this is ofcourse if u didn't aasign them a value ] , and now the second result is true,

now i didn;t say that int x[100] = {100}, will assign the whole elemnts to 100,
otherwise i said that int x[100] = {0} will assign the whole elemnts to zeros, and this is true man.
that is the differances