-1

As the question says. Is there some difference between the content of them?

Why do we have to declare the variable type before the pointer ????

Does the c process them differently?

Exemple:

typedef struct{
    int a;
    char b;
} some_st;

int main(){
    some_st *p;
    int *q;
}

Just a note: I came up with this question after work with linked lists.

Thank you for reading and answering.

mattias
  • 2,079
  • 3
  • 20
  • 27
mateus A
  • 1
  • 3

3 Answers3

1

the pointers are the same size, but one points to an int and the other one to a struct. The reason to declare them accordingly is that the compiler can tell you when you use them incorrectly.

if you think you're above that, you can use void* for all your pointers, or use a language with even less type checking.

Aganju
  • 6,295
  • 1
  • 12
  • 23
0

C doesn't define how pointers are represented.

Pointers of different types may have different sizes, different alignments requirements and different bit representaion.

For example this is a completely valid result of an implementation:

sizeof( int* ) != sizeof( long* )

as is :

sizeof( int* ) != sizeof( some_st* )
2501
  • 25,460
  • 4
  • 47
  • 87
0

There is no difference in the size or "structure" of the pointer itself, however the declaration does affect two things:

  1. It tells the compiler how to handle the value being pointed to (int or struct or whatever) - that is, what to assume the pointed type is.

  2. The way pointer arithmetic is being performed. That is what memory location ptr++ or ptr[4] evaluates to. For example, Assuming the struct has, say 12 bytes size, ptr++ will result in the address being incremented by at least 12.

Avi Perel
  • 422
  • 3
  • 8