469,923 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Passing INT Array by reference

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[i]);
*numArray[i]+=1;
printf("numArray[%d]=%d\n\n",i,numArray[i]);
}
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[i]);
}

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[i]);
}
printf("Now updintArray\n");
updintArray(&numArray);
return(0);
}

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

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

Nov 15 '05 #1
7 2761
On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jf********@gmail.com> wrote:
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[i]);
*numArray[i]+=1;
printf("numArray[%d]=%d\n\n",i,numArray[i]);
}
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[i]);
}
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[i]+=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>>
Nov 15 '05 #2


Barry Schwarz wrote:
On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jf********@gmail.com> wrote:
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[i]);
*numArray[i]+=1;
printf("numArray[%d]=%d\n\n",i,numArray[i]);
}
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[i]);
}
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[i]+=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.

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


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[i]);
/* *numArray[i] += 1; */
numArray[i] += 1;
/* was: format '%d' expects type 'int', but argument 3
has type 'int *' */
printf("numArray[%d]=%d\n\n", i, numArray[i]);
}
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[i]);
}

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

Nov 15 '05 #3


Jeff K wrote:
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[i]);
*numArray[i]+=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[i]);
}
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[i]);
}

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[i]);
}
printf("Now updintArray\n");
updintArray(&numArray);
return(0);
}

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

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


Nov 15 '05 #4
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++

Nov 15 '05 #5
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[i]);
numArray[i] += 1;
printf ("numArray[%d]=%d\n\n", i, numArray[i]);
}
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[i]);
}

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

Nov 15 '05 #6
On 14 Jul 2005 21:59:13 -0700, "Suman" <sk*****@gmail.com> wrote:


Barry Schwarz wrote:
On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jf********@gmail.com> wrote: snip
>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[i]+=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>>
Nov 15 '05 #7


Barry Schwarz wrote:
On 14 Jul 2005 21:59:13 -0700, "Suman" <sk*****@gmail.com> wrote:


Barry Schwarz wrote:
On 14 Jul 2005 13:59:51 -0700, "Jeff K" <jf********@gmail.com> wrote: snip >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[i]+=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.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dave | last post: by
3 posts views Thread by domeceo | last post: by
8 posts views Thread by kalinga1234 | last post: by
11 posts views Thread by John Pass | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.