initializing a pointer?

P

Paminu

In the following code I am trying to initialize a pointer that is located in
a struct.

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test
{
void *content;
struct test_ *bob;

} test_;




int main(void)
{

test_ *tt, *array;
tt =(test_ *) malloc(sizeof(test_));
array = (test_ *)malloc(sizeof(test_ *)*KIDS);
tt->bob = array;

return 0;
}

I would like the pointer called "bob" in struct "test_" to point to the same
thing that "array" points to. Actually I would like the to copy "array" to
"bob". When I compile the above code I get the error:

test2.c: In function 'main':
test2.c:21: warning: assignment from incompatible pointer type

how do I initialize the bob pointer with the array pointer?
 
C

Chris Dollin

Paminu said:
In the following code I am trying to initialize a pointer that is located
in a struct.

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test
{
void *content;
struct test_ *bob;

You're sure_ that's what you mean?
 
P

Paminu

Chris said:
You're sure_ that's what you mean?


bob should be a pointer to a test struct. But since I use typedef and have
renamed my struct to "test_" I guess it should be a test_ pointer.
 
J

John Bode

Paminu said:
In the following code I am trying to initialize a pointer that is located in
a struct.

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test
{
void *content;
struct test_ *bob;

This is the source of your problem. "struct test_" is *not* the same
thing as the typedef name "test_". You've declared bob to a pointer to
a different (and as yet incomplete) structure type than the one you're
defining.

Declare bob as "struct test *bob" and the problem will go away.

structure tags and typedef names occupy different namespaces, so it's
possible for the identifier test_ to be associated with two different
types.
} test_;




int main(void)
{

test_ *tt, *array;
tt =(test_ *) malloc(sizeof(test_));

Don't cast the return value of malloc() -- you don't need to (unless
you're using an *old*, pre-C89 compiler), and doing so can hide
potential errors.
array = (test_ *)malloc(sizeof(test_ *)*KIDS);

This isn't doing what you want; instead of allocating 4 elements of
type test_, it's allocating 4 elements of type test_* (pointer to
test). For this reason, it's generally best to pass the thing you're
allocating to as the sizeof operand.

tt = malloc(sizeof *tt); // allocates 1 element of type test_
array = malloc(sizeof *array * KIDS); // allocates KIDS elements of
type test_
tt->bob = array;

return 0;
}

I would like the pointer called "bob" in struct "test_" to point to the same
thing that "array" points to. Actually I would like the to copy "array" to
"bob". When I compile the above code I get the error:

test2.c: In function 'main':
test2.c:21: warning: assignment from incompatible pointer type

how do I initialize the bob pointer with the array pointer?

Fix the struct definition as mentioned above.
 
M

Mark McIntyre

bob should be a pointer to a test struct. But since I use typedef and have
renamed my struct to "test_" I guess it should be a test_ pointer.

The _ on the end of sure was a hint to you.

Here's a larger one:
What is the name of the type you're creating?
Since you created a type, why would you need the struct keyword here?
Have you actually created the type at this point in your code?

Mark McIntyre
 
P

Paminu

John said:
This is the source of your problem. "struct test_" is *not* the same
thing as the typedef name "test_". You've declared bob to a pointer to
a different (and as yet incomplete) structure type than the one you're
defining.

Declare bob as "struct test *bob" and the problem will go away.

structure tags and typedef names occupy different namespaces, so it's
possible for the identifier test_ to be associated with two different
types.


Don't cast the return value of malloc() -- you don't need to (unless
you're using an *old*, pre-C89 compiler), and doing so can hide
potential errors.


This isn't doing what you want; instead of allocating 4 elements of
type test_, it's allocating 4 elements of type test_* (pointer to
test). For this reason, it's generally best to pass the thing you're
allocating to as the sizeof operand.

tt = malloc(sizeof *tt); // allocates 1 element of type test_
array = malloc(sizeof *array * KIDS); // allocates KIDS elements of
type test_


Fix the struct definition as mentioned above.


thank you the good explanation!
 
E

Emmanuel Delahaye

Paminu a écrit :
typedef struct test
{
void *content;
struct test_ *bob;

} test_;

'struct test_' is not defined. You want 'struct test'.

BTW, you don't need these underscores. Obviously, it only obfuscates the
code...

According to my coding rules :

typedef struct test
{
void *p_content;
struct test *p_bob;

}
test_s;
 
K

Keith Thompson

Paminu said:
In the following code I am trying to initialize a pointer that is located in
a struct.

#include <stdlib.h>
#include <stdio.h>
#define KIDS 4

typedef struct test
{
void *content;
struct test_ *bob;

} test_;
[snip]

As you know by now, you need "struct test" for the declaration of bob,
not "struct test_". So let's look at the corrected code:

typedef struct test {
void *content;
struct test *bob;
} test_;

This declares a type called "struct test" (one of whose members is a
pointer to a "struct test"). It also create an alias for that type
called "test_".

In your original code, since you haven't declared something called
"struct test_", the compiler assumes it's an incomplete type. cyou
can declare a pointer to an incomplete type as long as you complete
the type later, which is why the compiler didn't complain. (At that
point, "struct test" is itself an incomplete type, completed at the
end of the struct definition.) This is what makes it possible for two
or more distinct structure types to contain pointers to each other.

Since struct tags are in a separate namespace, you can use the same
identifier for the struct and the typedef:

typedef struct test {
void *content;
struct test *bob;
} test;

Now you have two names for the same type: "struct test" and "test".

Here's the question: why do you need two names? Opinions differ on
this point, but in my opinion the typedef is not particularly useful.
I would just declare the type like this:

struct test {
void *content;
struct test *bob;
};

and simply refer to the type as "struct test" everywhere. The typedef
saves you a little typing, but that's not a good enough reason to use
it; the keystroke shortage ended some time ago.

It makes sense to use a typedef for a structure if you want to hide
the fact that it's a structure; for example, code that uses the type
FILE in <stdio.h> doesn't need to know that it's a structure. That's
not the case here.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top