473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(t est_));
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 2102
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(t est_));
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(t est_));


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_Keit h) 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
19993
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
7
2438
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 "Null pointer assignment" error. Kindly help. Also, eventually I intend to move this logic to a separate function. For that, I believe, that I will need to pass a pointer-to-pointer-to-pointer type as an arguent. Please confirm.
5
10580
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 the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I initilize it by enumerating its values? - If I do: v = new vector<vector<int> >(3) for example, is it
13
27184
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
4790
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 structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
21
3109
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 means it points to an array,On incrementing the pointer,it should point to next array of that size. Correct me if i am wrong. We declare a pointer to an integer array of Size N as int (*a);
7
4068
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 array, and writes them out. But it fails and exits the program. I guess that it's about initializing the array but i couldn't find a way to make it ok....
11
2299
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
3374
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
2343
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 initialization takes place." Does this mean all non-local objects will be zero-initialized before they are initialized by their initializers(if they have)? For example: int g_var = 3; int main() {}
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.