-4

when you are declaring static variables in C,

say you wrote a program as such:

int *c;
void foo(){
    c = (int *) malloc (10*sizeof(int));
    c = &c[3];
    *c = *&c[3];
}

What does it mean to have *c? I mean I understand that * means that it points to something but what does *c do?

and also, why do you need to cast (int *) the return value of malloc()?

Yu Hao
  • 115,525
  • 42
  • 225
  • 281

2 Answers2

5

when you are declaring static variables in C

Not related to this question, or atleast to the code you've shown.

but what does *c do?

Assuming your question related to the statement *c = *&c[3];, it refers to the object at address held by c.

why did you have to cast (int *) in front of malloc?

You should not. Please do not cast the return value of malloc() [and family].

Note: You code snippet is very poor and bad practice, most likely to be invalid. c = &c[3]; obvious memory leak.

Community
  • 1
  • 1
Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
0

I mean I understand that * means that it points to something but what does *c do?

The * does not always mean that it point to something. Example: If you write this:

int* c;

It means c is a pointer points to an int variable.

When you do like this:

int* c = &x;
*c = 5;

The second * is pointer c dereference.

Community
  • 1
  • 1
Trevor
  • 5,489
  • 2
  • 17
  • 43