By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

modify structure array pointer in the function

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
s88 wrote:

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 */

--
pete
Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
On 20 Nov 2005 18:10:30 -0800, "s88" <da*****@gmail.com> wrote:
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.
ptr++;
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>>
Nov 21 '05 #4

P: n/a
"s88" <da*****@gmail.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 21 '05 #5

P: n/a
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 */
s88 wrote:
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.