-6
typedef struct{
int x;
int y;
} Coordinate_T;

Coordinate_T *p;
p = (Coordinate_T *)malloc(sizeof(Coordinate_T));
    
p->x = 100;
p->y = 200;
    
free(p);
    
exit(0);

I'm trying to get the hang of pointers but it is really confusing when they are used like this. I got this code from a textbook and I'm supposed to find what's wrong with this code

Steve
  • 7
  • 2
  • Where did you find that? *p has no member x or y, *p is not even a struct or union to begin with. – lulle Oct 29 '21 at 22:08
  • sorry I for some reason replaced coordinate_t with int – Steve Oct 29 '21 at 22:12
  • 1
    You left out the definition of Coordinate_T – lulle Oct 29 '21 at 22:13
  • 1
    We'll need to see rather more of the code. And what it is that you don't understand - is it the `->` operator? – Adrian Mole Oct 29 '21 at 22:13
  • 1
    It seems you are misquoting and/or taking the original text out of context. Please provide more of the original context/wording and also explain what specifically you don't understand. – kaylum Oct 29 '21 at 22:16
  • sorry folks, forgot to put in that one – Steve Oct 29 '21 at 22:20
  • 1
    At least 4 missing semicolons. – Adrian Mole Oct 29 '21 at 22:28
  • What exactly do you find confusing? Probably the arrow operator (->)? if so, see https://stackoverflow.com/questions/2575048/arrow-operator-usage-in-c – lulle Oct 29 '21 at 22:32
  • The textbook is saying that there is something wrong with the pointer or the code, I'm really not seeing any issue but apparently there is – Steve Oct 29 '21 at 22:33
  • 1
    What would happen if malloc failed and returned NULL? – lulle Oct 29 '21 at 22:38
  • I think the whole point of the question was to emphasize if you don't put the struct in the code, the code won't run. Very confusing textbook, probably gonna ditch it. Thank you for trying to help though. – Steve Oct 29 '21 at 22:41

1 Answers1

0

Woo, it's been a long time since I dealt with C pointers, but let me see if I can help.

Coordinate_T *p;

declares a pointer to a Coordinate_T struct. We don't have any memory available yet, we just have a pointer to... nothing.

p = (Coordinate_T *)malloc(sizeof(Coordinate_T))

actually allocates the memory for us. Now p points to something useful where we can store values. "malloc" is shorthand for "memory allocation." It requires a size - how much memory do you need? "sizeof(Coordinate_T)" is an easy way to say "the size of this struct that I want to point to. Finally, the type cast "(Coordinate_T *)" tells the compiler "treat this like a Coordinate_T pointer".

p->x = 100;
p->y = 200;

Sets the x value of our newly-allocated struct to 100, and the y value to 200. The arrow notation (->) says "p is a pointer; inside the memory it points to, set..."

free(p)

frees the memory you just allocated with malloc(). This means we're done with it, the operating system can use that memory for something else. If you don't free memory when you're done with it, that's a "memory leak" - it's still marked as being in-use, and the operating system can't re-use it. In a long-running program, the leaked memory can build up and build up, and eventually the operating system kills the running program when no more memory is available.

exit(0);

just kills the program, and returns the value zero, which is the traditional value that means "Everything is fine." If a program returns any other value, that means some error occurred.

tl;dr: This program doesn't do much. It sets a couple values, then throws away the memory it used, and exits. It would be more interesting to include a printf() statement to echo those values back to you, but I'll leave that to you. ;-)

Mike Waldron
  • 144
  • 8
  • 1
    I think this is the right answer, I think the textbook was trying to emphasize that if you don't put a struct in the code, the code won't work. Very confusing question, but thank you for the explanation – Steve Oct 29 '21 at 22:39