0

I'm writing a code to manipulate a linked list. But It won't compile because on line 36 i'm making an integer from a pointer without a cast. I'm not sure why this is happening. But it's affecting my function "Ins" which puts new characters into my list. Let me know what you think.

Please ignore functions del, fde, pst, prl, pcr, ppr, and psu. I haven't gotten to those yet, they shouldn't get in the way. Thanks

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

#define MIN_LENGTH 4
#define MAX_LENGTH 11

struct node{
     char list;
    int count;
       struct node *next;
 };
typedef struct node Node;
typedef Node *ListNode;

void ins(ListNode *ptr, char value);

int main(void){

  ListNode startPtr = NULL;

  char com[MIN_LENGTH];
  char cho[MAX_LENGTH];

  while(strcmp(com, "end") != 0){
    printf("Command? ");
    scanf("%s", &com);

     if(strcmp(com, "ins") == 0){
       scanf("%s", &cho);
       ins(&startPtr, cho);
       printf("%s", cho);

        }

      else if(strcmp(com, "del") == 0){
    // del();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "fde") == 0){
    // fde();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "pst") == 0){
    // pst();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "prl") == 0){
    // prl();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "pcr") == 0){
    // pcr();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "ppr") == 0){
    // ppr();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }
      else if(strcmp(com, "psu") == 0){
    // psu();
       scanf("%s", &cho);
       printf("%s\n", cho);
        }

    else if(strlen(com) >= 4 || strlen(com) < 3){
    printf("You have entered an incorrect command.\n");
    }
  }
}


void ins(ListNode *ptr, char value){

  ListNode newPtr;
  ListNode prevPtr;
  ListNode currPtr;

    newPtr = (struct node*) malloc(sizeof(Node));

    if(newPtr != NULL){
    newPtr->list = value;
    newPtr->next = NULL;

    prevPtr = NULL;
    currPtr = *ptr;

    while(currPtr != NULL && value > currPtr-> list){
      prevPtr = currPtr;
      currPtr = currPtr->next;
    }
    if(prevPtr == NULL){
      newPtr->next = *ptr;
      *ptr = newPtr;
    }
    else{
      prevPtr->next = newPtr;
      newPtr->next = currPtr;
    }
   }
    else{
      printf("No memory available\n");
    }
}
void del(){
}
void fde(){
}
void pst(){
}
void prl(){
}
void pcr(){
}
void ppr(){
}
void psu(){
}
3
  • Which line is giving you trouble? It isn't clear which line is 36 (I counted and the error didn't make sense to me). Commented Oct 25, 2013 at 2:22
  • " ins(&startPtr, cho); " Is the line causing trouble. Commented Oct 25, 2013 at 2:27
  • 1
    You asked a very similar question a little while ago, I'd suggest just trying a little harder, instead of defaulting to coming straight onto Stack Overflow every time you run into a problem. Commented Oct 25, 2013 at 2:30

3 Answers 3

1

You have cho as a char array, you have function ins accepting two parameters, the second being a char. Then you try to call function ins with the second parameter being cho. As cho is a char array, its name is considered to be equivalent to a pointer. So you are calling ins with the second parameter being a pointer, whereas that function expects a char, which is an integer type. You cannot reasonably expect the compiler to convert that pointer to that char.

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

4 Comments

so If i were to remove the [Max_LENGTH] from char cho; would it heal this problem?
It would heal this problem, AND at the same time would cause your program to crash when reaching any of the scanf("%s", &cho) lines.
I see that... Interesting. This is a new problem. Do you have any insight on how to resolve?
You should read about what scanf does, and how it works. I suggest some basic C programming book, like this: en.wikipedia.org/wiki/The_C_Programming_Language
0

if you need to store strings, you can read here:

you need to change your structure to:

struct node{
     char *list;

then change method:

void ins(ListNode *ptr, char *value);

then change insert logic:

newPtr->list = (char*)malloc(strlen(value)+1); // NOTE: dont forget to free this memory
memcpy(newPtr->list, value, strlen(value)+1);

then, you have problem:

while(currPtr != NULL && value > currPtr-> list)

I don't understand what you're trying to compare here, if you need to compare strings - use strcmp here instead of >

if you need to store individual chars, read here:

change you insertion logic to:

char *tcho = cho;
while (!*tcho) {
    ins(&startPtr, *tcho);
    tcho++;
}

2 Comments

I got the impression he wanted each node to store one individual character, and insert them to leave the list in a sorted order, but it's hard to tell since he never explains what he's trying to achieve.
@PaulGriffiths yeah, it makes sense
0

Declare your struct node to contain a char*,

struct node{
    char* list;
    int count;
    struct node *next;
};

Change your ins() function declaration,

void ins(ListNode *ptr, char* value);

Call ins() with correct parameters,

main(void) {
//...
       ins(&startPtr, cho);
//...
}

define your ins() function to allocate space for the data string passed,

void ins(ListNode *ptr, char* value){
//...
    newPtr->list = strdup(value);
//...
}

You have three pointers declared in ins(), but typical single linked list inserts only need two, one to hold the new node, and one to hold the iterator to scan for the end of the list,

    ListNode newPtr;
    ListNode prevPtr; //we will omit this
    ListNode currPtr;

After looking a little closer, the newPtr is your newly allocated list Node which you malloc, and check for successful malloc (good). And you seem to be using prevPtr and currPtr as a way to walk the list looking for the end of the list (often called the tail, while the front of the list is often called the head). This is a workable solution, but you can simplify it.

Here is a function to construct a new Node,

ListNode NodeNew(char* value)
{
    ListNode newPtr;
    if( (newPtr = (struct node*) malloc(sizeof(Node))) == NULL )
    {
        printf("ENOMEM memory available\n");
        return newPtr=NULL;
    }
    newPtr->list = strdup(value);
    newPtr->next = NULL;
    newPtr->count = 0;
    return newPtr;
}

And here is a simpler list insert,

void ins(ListNode *head, char* value)
{
    ListNode newPtr;
    ListNode prevPtr;
    ListNode currPtr;

    if( !(newPtr = NodeNew(value)) )
    {
        return;
    }
    if(*head == NULL)
    {
        *head = newPtr;
    }
    else
    {
        for( currPtr=*ptr; currPtr->next != NULL; )
        {
            currPtr = currPtr->next;
        }
        currPtr->next = newPtr;
    }
}

And you want to implement the list print (prl),

void prl(ListNode head){
    ListNode currPtr;
    for( currPtr=head; currPtr != NULL; )
    {
        printf("n:%s\n",currPtr->list);
        currPtr = currPtr->next;
    }
}

Comments

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.