user2917840
user2917840

Reputation: 11

Makes integer from pointer without a cast

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(){
}

Upvotes: 0

Views: 581

Answers (3)

ChuckCottrill
ChuckCottrill

Reputation: 4444

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;
    }
}

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24146

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++;
}

Upvotes: 0

Laszlo Valko
Laszlo Valko

Reputation: 2743

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.

Upvotes: 1

Related Questions