Reddevil February 2016

Pointers in C, how to interpret them?

As a person migrating from Java to C recently, I am struggling to understand certain concepts, two of which are pointers and structs and when they are combined together, my head throws an unknown error -102 example:

struct node {
    int            data;
    struct node*   next;
};

What is node*? I know it's a pointer but why putting the keyword struct ahead of it? I mean if it was in Java the constructor would've been as follows:

public class Construct{
    private int whatever;
    private String doesntmatter;
    public Construct(int x, String y){
       this.whatever =x;
       this.doesntmatter= y;
    }
}

So: In contrast with Java constructors, how are C structs and pointer different? (any super texty-explanation is welcome)

Answers


reshad February 2016

Here the story is different, in this code:

struct node {
   int            data;
   struct node*   next;
};

struct node* next; is not the constructor of the struct node. Actually it is defined in the function or in the main function as follows:

int main(){

struct node *head;
struct node *tail;
head=(struct node*)malloc(sizeof(struct node));
tail=NULL;

head->data=1;
head->next=tail;

I hope you get the point of the example written above.


Joachim Pileborg February 2016

The struct keyword is needed to inform the compiler that you are declaring a variable that is a structure.

C have two different namespaces, basically where names can live. One namespace is the normal one, where variable and function names (among others) exist. Then there's the second namespace where structure, union and enumeration names live.

If you don't use the struct keyword when declaring a variable, the compiler will look only in the normal namespace to find a type-alias (defined using typedef). If the name is not found then you will get an error. By using the struct keyword you tell the compiler to look in the other namespace for the name of a structure.


You can put the name of the structure in the "normal" namespace by creating a type alias using the typedef keyword. Taking your node structure example:

typedef struct node my_node;

That adds the name my_node in the normal namespace, as an alias for struct node. Note that since the structure name and the type-alias name lives in different namespaces, you can actually declare both to have the same name:

typedef struct node node;


FUZxxl February 2016

In the C programming language, a structure is a chunk of memory with different kinds of data in it. As opposed to Java, the members of a structure are directly placed in the structure. There is no indirection. In fact, there is never implicit indirection in the C programming language. Data is laid out exactly as you specify.

A pointer is a memory address (usually) which points to an object. You can use it to explicitly specify indirection where needed.

As Joachim Pileborg already pointed out, the struct keyword is needed to tell the compiler “lookup this name in the structure name space.” It has to be placed in front of names of structure types. You can use typedef to make an alias for a structure type in the normal name space so you can avoid writing struct all over the place but I don't recommend that.


John Bode February 2016

struct node { int data; struct node* next; };
...
What is node*? I know it's a pointer but why putting the keyword struct ahead of it?

You are defining a new data type named struct node with two elements named data and next; the element next is a pointer to another instance of struct node (the declaration is parsed as struct node (*next)). It's just a node in a simple, singly-linked list, like so:

+---+
|   |      data
+---+
|   | ---+ next
+---+    |
 ...     |
+---+    |
|   | <--+ data
+---+
|   | ---+ next
+---+    | 
 ...     |
+---+    |
|   | <--+ data
+---+
|   | ---+ next
+---+    | 
 ...    ...

etc.

In contrast with Java constructors, how are C structs and pointer different?

Well, given that C doesn't have constructors of any sort, they're completely different. A struct is just a data type with multiple attributes, laid out sequentially. C struct types don't have any methods or executable code associated with them; they're just wads of bytes with some internal structure.

Post Status

Asked in February 2016
Viewed 3,935 times
Voted 4
Answered 4 times

Search




Leave an answer