Question

I have a problem with my code, I keep on getting the compiler error:

error message : warning: assignment makes pointer from integer without a cast

My code is simple:

int i;
int arr1[8], arr2[8];
char test[] = "1|a|b;2|c|d;3|e|f";
char *split1, *split2;

split1 = strtok(test, ";");
for(i = 1; i <= 8; i++){
  arr1[i] = split1;
  split1 = strtok(NULL, ";");
  printf("result=%s\n", arr1[i]);
}

When I fix the compiler error, my code can't print value and vice versa!

Was it helpful?

Solution 2

Besides the error mention in your question, you also have lots of other errors. For example:

arr1[i]=split1;

The variable split1 is a string, a pointer to char, while arr1 is an array of int.

To convert a string containing a number to an integer, you can use strtol.

If that's not what you want, you need to make arr1 an array of strings, i.e. an array of pointer to char:

char *arr1[8];

You also have a more serious error once you get the program to compile: Array indexes are zero based. That means that array indexes go from zero to the size minus one, or in the case of arr1 the indexes are from 0 to 7 (inclusive). Indexing an array out of bounds leads to undefined behavior.

Another case of undefined behavior is this line

printf("result=%s\n",arr1[i]);

Here you want to print a string, but provide an integer. This is not applicable if you change arr1 to an array of strings, of course.

OTHER TIPS

error message : warning: assignment makes pointer from integer without a cast

You need to include the proper prototype for the compiler to know what types the function returns (or it will assume they return int).

The prototype for strtok() is in <string.h>.

#include <string.h>

    split1=strtok(NULL,";"); // without a prototype
                             // the compiler assumes strtok() returns an int
                             // and complains about assigning the int to a pointer

Well compiler warns you that it is wrong to assign pointer to an int value. First because pointer can be greater size then int. In your case - you need to assign not a pointer but value referenced by it ( so just use arr1[i] = *split1;

But first check if split1 is not NULL, if you dereference NULL value you'll get access violation and you app will crash.

Btw, I recommend you to pay attention on previous answers, as they contain a lot of useful information for You.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top