-2

I can't understand why this part of code doesn't build and run... I have checked it again and again but I can;t find the problem.The problem is at Insert void 2nd line.

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

struct Node* head;

void Insert(int x){
    struct Node* temp = (Node*)malloc(sizeof(struct Node));
    (*temp).data = x;
    (*temp).next = NULL;
}

void Print(){
    struct Node* temp = head;
    printf("List is :\n");
    while (temp != NULL){
        printf("%d",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main() {
    head = NULL;
    printf("How many numbers ?\n";)
    int n,i,x;
    scanf("%d", &n);
    for (i=0;i<=n;i++){
        printf("Enter the number \n");
        scanf("%d",&x);
        Insert(x);
        Print();
    }
    return 0;
}
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
Nmpal
  • 11
  • 3

4 Answers4

2
  1. The -> operator is better to use with struct pointers.

  2. Do you want to use this node after calling the method? If yes, try to return the pointer.

  3. As @dbush pointed, you did not use the typedef keyword. Therefore, you need to change the 'Node*' in your casting to 'struct Node*'.

Dvir Hatuka
  • 21
  • 1
  • 3
  • `The -> operator is better to use with struct pointers.` There is no reason to say that. The `->` Operator is there for a better "understanding" and not for being better than `(*).` – Michi Jun 26 '18 at 16:39
  • 1
    @Michi: There is every reason not to use `(*(*(*(*ptr1).ptr2).ptr3).ptr4).member` instead of `ptr1->ptr2->ptr3->ptr4->member` (assuming there's a justification for having such nested structures in the first place). Notationally, the arrow notation `ptr->member` is dramatically superior to the `(*ptr).member` notation; doubly so when there's more than one level. – Jonathan Leffler Jun 26 '18 at 17:00
  • @JonathanLeffler I am agree. But We can not say that is **"better"** to use `->` instead of `(*).` – Michi Jun 26 '18 at 17:02
  • @Michi — yes, we can say that it is better to use arrow than parentheses-star-dot to a beginner. If they learn to program according to orthodox rules, life will be easier for everyone (us, them, their tutor, their colleagues). It is helpful to point out stylistic issues — I routinely point out that neither dot `.` nor arrow `->` should have spaces around them; they bind extremely tightly and should be written to match. – Jonathan Leffler Jun 26 '18 at 17:04
  • @JonathanLeffler Agree again. I remember tree years ago when I started to learn `C` and people here told me to not use something like this `*( *( a + i ) + j )` and I should use `a[i][j]`. I needed more than 6 months to understand Pointers just because I decided to not use `*( *( a + i ) + j )`. So from my (still) beginner side, I'm going to stick with Pointer notation. – Michi Jun 26 '18 at 17:08
  • 1
    @Michi: Your choice. I find `*(*(a+i)+j)` (with or without the extra spacing) ridiculously hard to read compared with `a[i][j]` (which needs no extra spacing). I'd strongly counsel against using the star-parentheses notation for array subscripting too — I nearly brought it up in one of my previous ripostes, but decided against. Evidently, I should have. – Jonathan Leffler Jun 26 '18 at 17:11
  • @JonathanLeffler Thank you for that. Could you please leave me an explanation [HERE](https://stackoverflow.com/questions/51043235/strange-output-issue-in-c/51043333#51043333)? On Lundin's Answer. – Michi Jun 26 '18 at 17:13
  • 2
    @Michi: I need to go to the office and pretend to get some work done. I aim to follow up later. Using `&a` where `a` is an array (which is what the other question seems to be about) is subtle (as in, not normally explained in beginning C courses because it fries the mind struggling to learn pointers at all). Given `int a[5];`, `&a` is of type `int (*)[5]`, which is quite different from `int *` as you can see from the spelling. If it's any consolation, pointers to functions are even more abstruse — and the abuses of `*` and `&` associated with them are weirder still. – Jonathan Leffler Jun 26 '18 at 17:19
0

I believe you want to achieve something like this:

#include <stdlib.h>

typedef struct tag_Node Node;

struct tag_Node {
    int data;
    Node* next;
};

void Insert(Node *to, int x) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = x;
    node->next = 0;
    to->next = node;
}

Assuming that you want to implement a linked list, you should have an additional parameter in your Insert method, which will specify the node you should insert your next value to. You will also need to use struct dereference operator (->) if you work with pointers.

onestep.ua
  • 118
  • 7
  • 2
    this code doesn't even compile - the typedef is missing a name and you can't reference `Node*` inside the struct before you've typedef'd it..... – Morten Jensen Jun 26 '18 at 15:41
  • What compiler do you use? Compiled and works fine with MSVC. – onestep.ua Jun 26 '18 at 15:47
  • MSVC is a C++ compiler. This question is regarding C. This code will not compile with any standards compliant C compiler. – Morten Jensen Jun 26 '18 at 16:02
  • Thanks for the feedback, I've updated my answer after you first comment. You can disable C++ extensions in MSVC, so it will act like a C compiler. – onestep.ua Jun 26 '18 at 16:10
  • 1
    Note that the standalone `struct tag_Node;` line isn't necessary; the subsequent `typedef struct tag_Node Node;` does the same job (again). The standalone line would be necessary if there was a function prototype like `int count_Nodes(struct tag_Node *list);` before the `typedef`. – Jonathan Leffler Jun 26 '18 at 17:14
0

You have

struct Node* node = (Node*)...;

C's type system is a bit less forgiving than it used to be: it won't promote this thing Node* into a struct Node*.

You need

struct Node* node = (struct Node*)...;

or you could rely on void*'s promotion rules, which on modern compilers will promote to any pointer type.

struct Node* node = ...; /* assuming ... returns a void* */
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

There seems to be two things wrong with your code.

You're using (Node*) in the cast where it should have been (struct Node*) or you can even omit it.

void Insert(int x){
    struct Node* temp = (Node*)malloc(sizeof(struct Node));
    (*temp).data = x;
    (*temp).next = NULL;
}

You have an error with your semi-colon in this statement as well:

printf("How many numbers ?\n";)

Replace with

printf("How many numbers ?\n");
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55