As a pointer, your "ptr" variable is simply a memory address that is pointing to god only know where at the start of your program. By attempting to set the "a" member variable of the struct, you may end up accessing areas of memory that are forbidden and could cause a program to crash. You need to actually allocate some space dynamically using some flavor of the "malloc" function or "new" operator and assign the address of the allocated space to your "ptr" variable before you start to use it.
Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
struct linkedlist
{
int a, b, c;
struct linkedlist *next_linkedlist;
}
int main()
{
// Create a variable and initialize it to point to an area in memory
// of the appropriate size.
struct linkedlist *ptr = malloc( sizeof(struct linkedlist) );
// Now you can assign values to the struct members.
ptr->a = 10;
// It is an EXTREMELY good idea to always initialize any pointers
// in a newly allocated link node to be NULL. Try to do this as
// soon as possible after creating the nodes.
ptr->next_linkedlist = NULL;
// Since the ptr->next_linkedlist pointer doesn't point anywhere,
// we must now create another node again using malloc before
// we can do any assignments to its members.
ptr->next_linkedlist = malloc( sizeof(struct linkedlist) );
// Now, like before we can assign values to the second node
// in the list.
ptr->next_linkedlist->next_linkedlist = NULL;
ptr->next_linkedlist->a = 37;
// Do you printf statements.
printf("%d\n",ptr->a);
printf("%d",ptr->next_linkedlist->a);
getch();
// Now we should return our dynamically allocated memory back
// to the system before we exit. Do this in reverse order from
// that used to allocate the nodes.
free(ptr->next_linkedlist); // Free up last node first
free(ptr); // Free up first node last
// Exit program.
return 0;
}
Most of the time you will want to write functions to add new nodes to the list and delete the list when finished instead of writing everything down manually in the "main" function, for your functions to be able to deal with your struct, the definition for the struct should be global which is why I moved it out of the "main" function. Doing things the way you were doing gets very tedious if you are adding lots of nodes to your list. If you need some guidance on how to write these functions I may be able to help.
As to your question about why the variables a, b, and c are not overwritten, they are in different areas of memory. The first node we create using "malloc" is at one memory address and the variables a, b, c, and next_linkedlist are stored at offsets to that memory location. When we create the second node again using a call to "malloc", this new node gets created at a different memory location from the first and all the offsets to this second node's member variables point to different memory areas than the first nodes' variables. So, nothing gets overwritten if you do things properly.
Better yet, learn some C++ since it seems to have built in "list" and "vector" templates that handle this sort of thing for you.