3

How do I do this? For example

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

Is my node, and in my main I have

       int main(){
          node* array[10];
          if (node[0].data == 0){
           ...

                                }

I am not really sure what I need to do here. I want to be able to check if these entries in the array have been modified already. How would I do this? I have tried the -> operator instead of . which is causing me some confusion because I am working with an object aren't I? I am uncertain of this.

Following advice this is what I now have.

 int main() {
 struct node* arr[10] = { 0 } ;
   addTo(data, arr);
            }


   addTo(int data, node* arr){
        if (arr[0] == NULL)
                             }

The last line is a segmentation fault.

7
  • 2
    The last line segfaults because the declaration is wrong: struct node* arr[10]; declares an array of 10 pointers, but doesn't initialize those pointers to valid memory. You meant struct node arr[10]; to get an array of actual structures. Commented Oct 8, 2013 at 15:17
  • @Unwind I have tried that and when I do I just get the seg fault on the original line I was getting it from my original question. Commented Oct 8, 2013 at 15:20
  • and what is this hashT[n] ? you need to use with arr[n] instead. As @unwind said allocate memory or other wise declare struct node arr[10]. Commented Oct 8, 2013 at 15:20
  • @Gangadhar Oops, mixing test code with other code. Commented Oct 8, 2013 at 15:25
  • if (arr[0].data == 0) is still giving me the segmentation fault with the method described by unwind. Commented Oct 8, 2013 at 15:26

3 Answers 3

4

Arrays in C cannot be "empty". They are never empty. If you declared an array of 10 elements you will always have an array of 10 elements. There's no way to say whether some element has been modified or not unless you yourself come up with some way to manually "mark" elements that you modify. For example, you can choose some reserved element value, which will designate an "empty" element.

In your case you declare an array of 10 pointers

node* array[10];

If you supply an initializer

node* array[10] = { 0 };

your array elements will have the initial value of null. You can use that value as a mark of an "empty" element.

Sign up to request clarification or add additional context in comments.

6 Comments

Then why am I getting a segmentation fault when trying to access data at specific indices?
If you just accessed array elements (i.e. pointers), everything would be fine. But you go further: you attempt to dereference array elements and access the data there. That's what causes the crash, since the array elements have not been assigned any meaningful values yet. You can't dereference them.
But first you need to decide what it is you need: an array of pointers to nodes or an array of nodes themselves.
I am not sure what the difference would be, I wan an array of linked lists
@Paul the Pirate: Great. In that case an array of pointers is a viable idea (unless you want a list with a dedicated "header" element). Initialize your array with nulls at the start. Null pointer would stand for an empty list. So, just don't forget to check for null before accessing anything.
|
2
       node array[10]; //array of struct node type 
       //node *array[10]; //this becomes declaration of array of pointers

       //assuming you have initialized all with 0.

       for(i=0;i<10;i++)   
       if (array[i].data == 0)   
       //data modified.
       //if you declare array of pointers check if(array[i]->data==0)

5 Comments

How did you declared array
I made it a node* array[10]
if you see answer clearly then you will found what is your mistake.
Well I tried node array[10] also but I still get the same error. Is there perhaps a problem with how C creates an array and that my nodes are too large or something? I have no idea. I am just guessing but I feel like the pr oblem is that it is trying to access a data point in node that doesn't exist because node doesn't exist yet. I am getting a segmentation fault.
I updated the question and I am still having t rouble getting it to work.
1

The code you posted cannot lead to a crash, because it doesn't even compile. I fixed all the obvious mistakes to get rid of the compiler errors, and now it works flawlessly here without crashing:

#include <stdio.h>
#include <stdlib.h>

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

/* changed node* to node*[] and added return type */
void addTo(int data, node* arr[])
{
    if (arr[0] == NULL)
    {
        puts("yes");
    }
    else
    {
        puts("no");
    }
}

int main()
{
    struct node* arr[10] = { 0 } ;
    /* changed data to 42, because there is no variable data in scope here */
    addTo(42, arr);
}

If you have any further questions, feel free to ask.

1 Comment

Not sure what is going on but if (arr[0] == NULL) gives me a segmentation fault

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.