Jump to content
  • Advertisement
Sign in to follow this  
serious_learner07

problem with pointer to struct.

This topic is 3871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a variable of type struct pointer,then What is the meaning of *structpointer->membervariable ( membervariable of type unsigned char*).In one of the source codes written by my senior, this type of defintion is used. Where we need this. Code is written below along with the output
struct sample
{
	unsigned char * x;
	unsigned char * y;
};
int _tmain()
{
    // TODO: Please replace the sample code below with your own.
   struct sample*  sample1 = (struct sample*) malloc(sizeof(struct sample));
   sample1->x = new unsigned char[10];
   sample1->y = new unsigned char[10];
   if(sample1==NULL)
	printf("LLLLLL");
   *(sample1->x+1) =  'd'; 
    printf("%d\n", sample1); // It is printing 114843128 
    printf("%u\n",*sample1->x); // It is Printing 205
    printf("%u\n",*sample1->y);  // It is Printing 205
    printf("%c\n", *(sample1->x+1)); //Printing d 
	
    Console::WriteLine(S"Hello World");
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
In your example you have *(sample1->x+1) where sample one accesses element x via a pointer the add one will access the memory location after the one pointed to by x then *() will dereference the memory location and store 'd' there. so if x was a straight char array it would be x[1] = 'd'.

the reason why *sample->x prints a random number is the fact that you haven't set it to anything.

Share this post


Link to post
Share on other sites
It is hideously complicated. An easier to read version would be:

sample1->x[1] = 'd';

In fact, the entire program is dubious:

0) checking for NULL from malloc after assigning to its members.
1) not doing anything useful if the test fails.
2) mixing new / new [] / malloc.
3) no delete / delete [] / free calls.
4) (apparently) using dynamic allocation on fixed length arrays.
5) using %d to print a pointer (should be %p).
6) using %u on a character.
7) allocating the struct on the heap, it could easily be placed on the stack.

Are you supposed to be writing C, C++ or C++.net?

Share this post


Link to post
Share on other sites
Quote:
Original post by serious_learner07

struct sample* sample1 = (struct sample*) malloc(sizeof(struct sample));
sample1->x = new unsigned char[10];
Console::WriteLine(S"Hello World");


...

...

...O_o

Er, like rip-off said. Which language do you think you're using?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!