modify structure array pointer in the function

S

s88

Howdy:
the follows is my program, I wanna change my structure array
pointer in the function "testfunc", but I fail..., I also try to call
the testfunc by reference, but the compiler says "test6.c:35: error:
incompatible type for argument 1 of `testfunc'". Can I make my purpose
in C?
and how?


typedef struct xxxx *xxxx_ptr;

typedef struct xxxx{
int just;
long for_the;
char *test;
}xxxx;

void testfunc(xxxx_ptr ptr){
ptr++;
}

int main(void){
xxxx XXXX[10];
xxxx_ptr ptr = &XXXX[0];
printf("%x\n",ptr);
testfunc(ptr);
printf("%x\n",ptr);
return 0;
}

Thank you all.
Dave.
 
P

pete

s88 said:
Howdy:
the follows is my program, I wanna change my structure array
pointer in the function "testfunc", but I fail..., I also try to call
the testfunc by reference, but the compiler says "test6.c:35: error:
incompatible type for argument 1 of `testfunc'". Can I make my purpose
in C?
and how?

typedef struct xxxx *xxxx_ptr;

typedef struct xxxx{
int just;
long for_the;
char *test;
}xxxx;

void testfunc(xxxx_ptr ptr){
ptr++;
}

int main(void){
xxxx XXXX[10];
xxxx_ptr ptr = &XXXX[0];
printf("%x\n",ptr);
testfunc(ptr);
printf("%x\n",ptr);
return 0;
}

Every oportunity to use typedef,
isn't necessarily a good one.

/* BEGIN new.c */

#include <stdio.h>

struct my_struct {
int just;
long for_the;
char *test;
};

void testfunc(struct my_struct **ptr)
{
++*ptr;
}

int main(void)
{
struct my_struct array[10];
struct my_struct *ptr = array;

printf("%p\n", (void *)ptr);
testfunc(&ptr);
printf("%p\n", (void *)ptr);
return 0;
}

/* END new.c */
 
R

rohin.koul

On the contrary when I copied your code into old Turbo C its compiled
and run just fine.
So I guess its a compiler issue
 
B

Barry Schwarz

Howdy:
the follows is my program, I wanna change my structure array
pointer in the function "testfunc", but I fail..., I also try to call
the testfunc by reference, but the compiler says "test6.c:35: error:
incompatible type for argument 1 of `testfunc'". Can I make my purpose
in C?
and how?


typedef struct xxxx *xxxx_ptr;

typedef struct xxxx{
int just;
long for_the;
char *test;
}xxxx;

void testfunc(xxxx_ptr ptr){

C passes arguments by value. The ptr is this function is a copy of
the argument value in the calling statement.

This updates the copy.

The copy is destroyed as part of the process of exiting the function.
The updated value no longer exists.
int main(void){
xxxx XXXX[10];
xxxx_ptr ptr = &XXXX[0];
printf("%x\n",ptr);

%x expects an unsigned int. You are passing a pointer. This invokes
undefined behavior.

The only portable way to print a pointer value is to use %p and cast
the value to a void *.
testfunc(ptr);

The value of an argument cannot be changed by the called program.
printf("%x\n",ptr);
return 0;
}

The two usual solutions are:

Have the function return the updated value and call the function
in an assignment statement that assigns the new value to a suitable
variable.

Pass the address of the variable to be updated and let the
function update the variable by dereferencing the address.


<<Remove the del for email>>
 
K

Keith Thompson

s88 said:
the follows is my program, I wanna change my structure array
pointer in the function "testfunc", but I fail..., I also try to call
the testfunc by reference, but the compiler says "test6.c:35: error:
incompatible type for argument 1 of `testfunc'". Can I make my purpose
in C?
and how?


typedef struct xxxx *xxxx_ptr;

typedef struct xxxx{
int just;
long for_the;
char *test;
}xxxx;

void testfunc(xxxx_ptr ptr){
ptr++;
}

int main(void){
xxxx XXXX[10];
xxxx_ptr ptr = &XXXX[0];
printf("%x\n",ptr);
testfunc(ptr);
printf("%x\n",ptr);
return 0;
}

Are you sure that's the actual code you fed to the compiler? When I
compiled it with gcc, I didn't get any diagnostics. When I
upped the warning level (gcc -ansi -pedantic -Wall -W -c tmp.c), I got:

tmp.c: In function `main':
tmp.c:16: warning: implicit declaration of function `printf'
tmp.c:16: warning: unsigned int format, xxxx_ptr arg (arg 2)
tmp.c:18: warning: unsigned int format, xxxx_ptr arg (arg 2)

You need a "#include <stdio.h>" to make the printf() calls legal.
Also, the correct format for a pointer is "%p", not "%x":

pirntf("%p\n", (void*)ptr);

(Since you got an error message on line 35, what you posted apparently
is shorter than the actual code.)
 
B

Blair Craft

Try this:
/* start */
typedef struct xxxx *xxxx_ptr;

typedef struct xxxx{
int just;
long for_the;
char *test;
}xxxx;

void testfunc(xxxx_ptr **ptr){
(*ptr)++;
}

int main(void){
xxxx XXXX[10];
xxxx_ptr ptr = XXXX;
printf("%x\n",ptr);
testfunc(&ptr);
printf("%x\n",ptr);
return 0;
}
/* end */
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top