473,425 Members | 1,691 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,425 software developers and data experts.

initializing a pointer?

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?

Feb 7 '06 #1
7 2074
Paminu wrote:
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?

--
Chris "try, or try not -- there is no do" Dollin
Feb 7 '06 #2
Chris Dollin wrote:
Paminu wrote:
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?

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.
Feb 7 '06 #3

Paminu wrote:
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.

Feb 7 '06 #4
On Tue, 07 Feb 2006 16:53:26 +0100, in comp.lang.c , Paminu
<sd**@asd.com> wrote:
Chris Dollin wrote:
Paminu wrote:
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?


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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 7 '06 #5
John Bode wrote:

Paminu wrote:
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.

thank you the good explanation!
Feb 7 '06 #6
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;

--
A+

Emmanuel Delahaye
Feb 7 '06 #7
Paminu <sd**@asd.com> writes:
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.

--
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.
Feb 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
7
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
21
by: rupesh_533 | last post by:
I am assuming the following things. 1.Pointer to an integer means it points to an integer,On incrementing the pointer,it points to the next integer in memory. 2.Pointer to an array of some size...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.