Sign in to follow this  

problem with pointer to struct.

This topic is 3594 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
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

This topic is 3594 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this