Hi, I'm trying to get started with C. I've done a bit of java programming, so starting with memory allocation and all that stuff is posing some problems. Can anyone give me some advice as to why this code:
gives this output:
No 0 has address 0x7fff5fbffa50
No 1 has address 0x7fff5fbffa50
No 2 has address 0x7fff5fbffa50
I want three structs with DIFFERENT addresses. My plan is to put these structs in a linked list, but its difficult when they just overwrite each other...
Code:
#include <stdio.h>
#include <stdlib.h>
int i = 0;
char nm = 'a';
struct Item {
char navn;
int alder;
struct Item *next;
};
void makeList(int k) {
int j;
struct Item *newPtr;
for (j = 0; j < k; j++) {
newPtr = malloc(sizeof(struct Item));
struct Item new = *newPtr;
printf("No %d has address %p\n", j, &new);
}
}
int main(int argc, char** argv) {
makeList(3);
return (EXIT_SUCCESS);
}
gives this output:
No 0 has address 0x7fff5fbffa50
No 1 has address 0x7fff5fbffa50
No 2 has address 0x7fff5fbffa50
I want three structs with DIFFERENT addresses. My plan is to put these structs in a linked list, but its difficult when they just overwrite each other...