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.
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.