Segment Violation

Hi to all.

I'm reciving a "Segment violation" error from this code and I don't know why.

void insertAtEnd(NodeType *pList) {

    char element;

    printf("Introduce a element: \n");
    setbuf(stdin, NULL);
    scanf("%c", &element);

    //Find the end of the list;
    while (pList->nextNode != NULL) {
        pList = pList->nextNode;
    }

    //Makes a new node.
    NodeType *newNode = (NodeType *) malloc(sizeof(NodeType));
    newNode->caracter = element;
    newNode->nextNode = NULL;
    //Links the new node.
    pList->nextNode = newNode;
}

Thanks for reading.

There are a no. of things that could be wrong...post the code that calls this function as well as the typedef of the struct NodeType.

The code thats calls this function is quite simple.

typedef struct node {

    char caracter;
    struct node *nextNode;
} NodeTipe;

int main(int argc, char argv[]) {
NodeTipe *pList;

insertAtEnd(pList);
}

The value of pList in main is undefined and probably pointing off into hyperspace somewhere. The instant you try to use it you're trying to access memory you never allocated.

Your logic is also a touch incorrect. An empty list has no memory allocated to it at all in this scheme, hence can't be modified without modifying the base pointer itself, which your function cannot do, being passed only a copy of it.

Pass a pointer to the pointer so it can modify it, check for the special case of creating the first node, and make sure you set the value of the empty list to NULL.

void insertAtEnd(NodeType **ppList) {
    NodeType *plist=(*ppList);
    char element;

    printf("Introduce a element: \n");
    setbuf(stdin, NULL);
    scanf("%c", &element);

    //Makes a new node.
    NodeType *newNode = (NodeType *) malloc(sizeof(NodeType));
    newNode->caracter = element;
    newNode->nextNode = NULL;

    // Empty list.
    if(plist == NULL)
    {
          // Set the base of the list to the new node
          (*ppList)=plist;
          return;
    }


    //Find the end of the list;
    while (pList->nextNode != NULL) {
        pList = pList->nextNode;
    }

    //Links the new node.
    pList->nextNode = newNode;
}

int main(int argc, char argv[]) {

    NodeTipe *pList=NULL;
    insertAtEnd(&pList); 
}
typedef struct node
{
    char caracter;
    struct node *nextNode;
} NodeTipe;

int main(int argc, char argv[])
{
     NodeTipe *pList;
     // you need code here to create the initial Node for the linked list and then start inserting to it
     insertAtEnd(pList);
}

also there's a big difference between NodeTipe and NodeType