How to use pointer to typedef struct !

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

I need to write value in struct. My problem is that I use typedef and I'm traing to solve this by using pointer to the struct. I'm not sure if this is the right way to solve this ?

My code:

typedef struct {				
	unsigned char age;
	unsigned char hair[10];
	unsigned char height;
	unsigned char name[20];
    } STUDENT;

STUDENT student_1, student_2;

void studentChange(char student_nr)
{ 
  char *p_student_nr;	//declear a pointer

  student_nr = 2;  // set some value

  switch (student_nr)
  {
   case 2:
      p_student_nr = &student_2; //store student_2 address to pointer
      break;
   default:
      break;
  }
  *p_student_nr.height = 180;  // write value to hight.

}

for the last code line:

*p_student_nr.height = 180;

I get following error :
left of '.height' must have struct/union type

I now that I can write value to struct by:

student_1.height = 180;

But I need to be able to change value for pre-defined student nr !

Does anyone now how to solve this ?

Best Regards
Rafeind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are declaring a char pointer, but what you want is a STUDENT pointer.

And I think using it involves the -> operator instead of * operator.

- Jani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd have thought you'd want the paramtere to studentChange to be a pointer to a struct of type STUDENT. That is:

void studentChange(STUDENT * this_student){

then to access a field of the record within the function:

age = this_student->age;
this_student->height = 180;

and you'd invoke the function with:

studentChange(&student_2);

However it's clear that you want to use an index to a database so rather than defining:

STUDENT student_1, student_2;

you might want to define:

STUDENT students[2];

then you could:

studentChange(1);

where:

void studentChange(unsigned char student_nr){

and within the routine:

students[student_nr].age = 5;

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In your routine you declare a pointer

char *p_student_nr;   //declear a pointer

and later point it to one of your STUDENT entries

p_student_nr = &student_2; //store student_2 address to pointer 

and finally try to access it

*p_student_nr.height = 180;  // write value to hight. 

Let's work backwards through the errors.

When you access a member of a structure form a pointer, you cannot use the 'dot' (.) notation you must use the 'points to' (->) notation. (As stated above) In addition, you will get another Error on that line once you fix the notation. You will get an error that says "age not a member of struct or union. This is because you declared the pointer as type 'char' thus the compiler thinks you are pointing to a char not a STUDENT.

p_student_nr->height = 180;

Next up, when you assign the address to the pointer. This would be correct, IF you had declared the pointer correctly. You should get a warning on this line about converting a pointer without a cast, or an error saying you are casting incompatible types. The reason is that "&student_2" returns a pointer to a STUDENT, and "p_student_nr" is declared as a pointer to a "char".

so now we get to your root error. The compiler won't complain on this line, as there is syntactically nothing wrong with it... you have just declared the pointer to the wrong type (but the compiler has no way of knowing this)
in order for those lines below to work, this pointer needs to be to type STUDENT, not char

STUDENT *p_student_nr;

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Cliff

You got the point ;)

Of course I want to use "index to database".

My code now :)

For those that need more info.

typedef struct {             
   unsigned char age; 
   unsigned char hair[10]; 
   unsigned char height; 
   unsigned char name[20]; 
    } STUDENT; 

STUDENT student[5];   //make 5 identical struct's

void studentChange(char student_nr)
{ 
  student[student_nr].height = 180;  // write value to hight.

}

This is much more simply to solve it with “index to database”. I don't need to use pointer and I can keep all my data in well organized structure.

Best Regards

Rafeind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's the joy of globals. You only need to start passing pointers around and using the indirection operator (->) when the data is kept local.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Glitch

Thanks for your explanation on my compiler error.

Best Regards
Rafeind