Passing INT Array by reference

J

Jeff K

Can you pass an int array by reference to a function and modify
selective elements?

Here is my code:

#include <stdio.h>

#define COLUMNSIZE 30
#define ASIZE 5
int calcfldpos(int *row, int *column, int *numArray)
{
int i=0;
printf("\tbefore-->++numArray=%d\n",*numArray);
*numArray+=4;
printf("\t after-->++numArray=%d\n",*numArray);
return(1);
}
int updintArray(int *numArray[5])
{
int i=0;
printf("\nupdintArray\n");
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
*numArray+=1;
printf("numArray[%d]=%d\n\n",i,numArray);
}
return(0);
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE];

numArray[0]=0; numArray[1]=1; numArray[2]=2; numArray[3]=3;
numArray[4]=4;

for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}

printf("before->calcfldpos:\tnumArray[2] = %d\n",numArray[2]);
calcfldpos(&row,&column,&numArray[2]);
printf(" after->calcfldpos:\tnumArray[2] = %d\n",numArray[2]);
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}
printf("Now updintArray\n");
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;

I do not know how the pointer are messed up?
Any comment would be appreciated.
 
B

Barry Schwarz

Can you pass an int array by reference to a function and modify
selective elements?

C passes by value exclusively. The technique used for arrays does
allow the called function to updated the arrays directly.
Here is my code:

snip

int updintArray(int *numArray[5])
{
int i=0;
printf("\nupdintArray\n");
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
*numArray+=1;
printf("numArray[%d]=%d\n\n",i,numArray);
}
return(0);
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE];

snip

for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}
printf("Now updintArray\n");


Please learn to indent consistently. It will save you (and us) a lot
of aggravation.
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;


How did you get past the compilation stage. updint is expecting an
array of 5 pointer to int (int *[5]). You pass it a pointer to an
array of 5 int (int (*)[5]). These types are not compatible an should
result in some diagnostic.
I do not know how the pointer are messed up?
Any comment would be appreciated.



<<Remove the del for email>>
 
S

Suman

Barry said:
Can you pass an int array by reference to a function and modify
selective elements?

C passes by value exclusively. The technique used for arrays does
allow the called function to updated the arrays directly.
Here is my code:

snip

int updintArray(int *numArray[5])
{
int i=0;
printf("\nupdintArray\n");
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
*numArray+=1;
printf("numArray[%d]=%d\n\n",i,numArray);
}
return(0);
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE];

snip

for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}
printf("Now updintArray\n");


Please learn to indent consistently. It will save you (and us) a lot
of aggravation.
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;


How did you get past the compilation stage. updint is expecting an
array of 5 pointer to int (int *[5]). You pass it a pointer to an
array of 5 int (int (*)[5]). These types are not compatible an should
result in some diagnostic.


He didn't.
Here are my efforts at fixing the code:
The warnings I found with gcc 4.0.0 are embedded in the code as well.

#include <stdio.h>

#define COLUMNSIZE 30
#define ASIZE 5
int
calcfldpos(int *row, int *column, int *numArray)
{
/* was: unused variable
int i = 0;
*/
printf("\tbefore-->++numArray= %d\n", *numArray);
*numArray += 4;
printf("\t after-->++numArray=%d\n", *numArray);
return (1);

}

int
/* updintArray(int *numArray[5]) */
updintArray(int numArray[5])
{
int i = 0;
printf("\nupdintArray\n");
for (i = 0; i < ASIZE; i++) {
/* was: format '%d' expects type 'int', but argument 3
has type 'int *' */
printf("numArray[%d]=%d\n", i, numArray);
/* *numArray += 1; */
numArray += 1;
/* was: format '%d' expects type 'int', but argument 3
has type 'int *' */
printf("numArray[%d]=%d\n\n", i, numArray);
}
return (0);
}

int
/* main(int argc, char **argv) */
main(void)
{
int row = 10;
int column = 0;
int i = 0;
int numArray[ASIZE];
numArray[0] = 0;
numArray[1] = 1;
numArray[2] = 2;
numArray[3] = 3;
numArray[4] = 4;

for (i = 0; i < ASIZE; i++) {
printf("numArray[%d]=%d\n", i, numArray);
}

printf("before->calcfldpos:\tn umArray[2] = %d\n",
numArray[2]);
calcfldpos(&row, &column, &numArray[2]);
printf(" after->calcfldpos:\tnumArray[2 ] = %d\n",
numArray[2]);
for (i = 0; i < ASIZE; i++) {
printf("numArray[%d]=%d\n", i, numArray);
}
printf("Now updintArray\n");
/* passing argument 1 of 'updintArray' from incompatible
pointer type
updintArray(&numArray);
*/
updintArray(numArray);
return (0);
}

This was compiled with:
gcc -std=c99 -Wall -pedantic -ansi test.c

Whether this is what the OP wanted to try out, I am not too sure.
 
R

ranjeet.gupta

Jeff said:
Can you pass an int array by reference to a function and modify
selective elements?

Call by refrence is In C++ not in C, In C their is only call by value
and below also in your code you have called by value :)

you have passed the copy of address, so passing the address (call
by value) we modify the content in the passed address
(call by value),

I hope now you are clear that there is nothing such as call by
refrence in C, Yes In C++ we have the Call by Refrence, Dont get
confuse....any way i did some modification in the code and
placed my comment also.
Here is my code:

#include <stdio.h>

#define COLUMNSIZE 30
#define ASIZE 5
int calcfldpos(int *row, int *column, int *numArray)
{
int i=0;
printf("\tbefore-->++numArray=%d\n",*numArray);
*numArray+=4;
printf("\t after-->++numArray=%d\n",*numArray);
return(1);
}
int updintArray(int *numArray[5])
^^^^^^^^^^^
change to *numArray // Why specifing index ?? when you
// are passing the base address of
the
// array in the main function.

int *numArrayPtr = numArray; // Declare the Pointer
// Which holds the
address
// of the passed
argument.
int i=0;
printf("\nupdintArray\n");
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
*numArray+=1;

^^^^^^^^^^^^^^
This will compile But Give the
memory allocation problem......

So solution is that you have to take the pointer varibale and replace
the above with this
*numArrayPtr +=1;

Regards
Ranjeet
printf("numArray[%d]=%d\n\n",i,numArray);
}
return(0);
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE];

numArray[0]=0; numArray[1]=1; numArray[2]=2; numArray[3]=3;
numArray[4]=4;

for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}

printf("before->calcfldpos:\tnumArray[2] = %d\n",numArray[2]);
calcfldpos(&row,&column,&numArray[2]);
printf(" after->calcfldpos:\tnumArray[2] = %d\n",numArray[2]);
for ( i=0; i<ASIZE; i++ )
{
printf("numArray[%d]=%d\n",i,numArray);
}
printf("Now updintArray\n");
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;

I do not know how the pointer are messed up?
Any comment would be appreciated.
 
E

Emmanuel Delahaye

Jeff K wrote on 14/07/05 :
Can you pass an int array by reference to a function and modify
selective elements?

Well, all parameters are passed by value in C. Arrays are kinda
special. WHat is passed is a copy of its address or of the address of
it's first element via some pointer to array or to element
rescpectively.

#1

f ((T *)arr[10])
{
}

#2
f (T arr[10])
{
}
or
f (T arr[])
{
}
or
f (T *arr)
{
}


The first way (pointer to array) keeps the size info, and the second
one (pointer to element) has no size info. An extra size parameter
should help...


#include <stdio.h>

typedef int T;

int f (T (*arr)[10])
{
printf ("sizeof arr[0] = %u\n", (unsigned) sizeof *arr[0]);
printf ("sizeof arr = %u\n", (unsigned) sizeof *arr);
return 0;
}

int g (T *arr)
{
printf ("sizeof arr[0] = %u\n", (unsigned) sizeof arr[0]);
printf ("sizeof arr = %u\n", (unsigned) sizeof arr);
return 0;
}


int main (void)
{

T a[10];

f (&a);
g (a);
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
E

Emmanuel Delahaye

Jeff K wrote on 14/07/05 :
<snipped some buggy code>

Code fixed and enhanced. Ask for details if you don't understand...

#include <stdio.h>

/* useful and reusable trick to get the number of elements of an array
* (I insist : *array*) ...
*/
#define NELEM(a) (sizeof(a)/sizeof*(a))

int calcfldpos (int *numArray)
{
printf ("\tbefore-->++numArray=%d\n", *numArray);
*numArray += 4;
printf ("\t after-->++numArray=%d\n", *numArray);
return (1);
}

int updintArray (int *numArray, size_t size)
{
size_t i;
printf ("\nupdintArray\n");
for (i = 0; i < size; i++)
{
printf ("numArray[%d]=%d\n", i, numArray);
numArray += 1;
printf ("numArray[%d]=%d\n\n", i, numArray);
}
return (0);
}

int main ()
{

int numArray[5] = {0,1,2,3,4};
size_t i;

for (i = 0; i < NELEM(numArray); i++)
{
printf ("numArray[%d]=%d\n", i, numArray);
}

printf ("before->calcfldpos:\tnumArray[2] = %d\n", numArray[2]);
calcfldpos (numArray);
printf (" after->calcfldpos:\tnumArray[2] = %d\n", numArray[2]);
for (i = 0; i < NELEM(numArray); i++)
{
printf ("numArray[%d]=%d\n", i, numArray);
}
printf ("Now updintArray\n");
updintArray (numArray, NELEM(numArray));
return (0);
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
B

Barry Schwarz

Barry said:
int updintArray(int *numArray[5])
{ snip
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE]; snip
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;


How did you get past the compilation stage. updint is expecting an
array of 5 pointer to int (int *[5]). You pass it a pointer to an
array of 5 int (int (*)[5]). These types are not compatible an should
result in some diagnostic.


He didn't.


Look again. numArray is an array of int. &numArray has type pointer
to array of int. The call to updintArray has an argument with an
incompatible type which requires a diagnostic.


<<Remove the del for email>>
 
S

Suman

Barry said:
Barry said:
int updintArray(int *numArray[5])
{ snip
}
int main(int argc,char** argv)
{
int row=10;
int column=0;
int i=0;
int numArray[ASIZE]; snip
updintArray(&numArray);
return(0);
}

I'm getting a core dumb on this statement:
*numArray+=1;

How did you get past the compilation stage. updint is expecting an
array of 5 pointer to int (int *[5]). You pass it a pointer to an
array of 5 int (int (*)[5]). These types are not compatible an should
result in some diagnostic.


He didn't.


Look again. numArray is an array of int. &numArray has type pointer
to array of int. The call to updintArray has an argument with an
incompatible type which requires a diagnostic.


What I meant was, he didn't get through the compilation stage.
I quoted more than necessary, which caused the confusion. I compiled
his code as is, and found some errors/warnings, which I *tried* to
fix (since I am not too sure of his intent), and posted.

Any confusion caused is regretted.

Suman.
 

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,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top