473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 3000
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
5
by: aelred | last post by:
I have a web page where a member can open up a chat window (child window) with another member. - From there the member can also navigate to other web pages. - From other pages in the site, they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.