I just started learning C a few days ago and I'm having a bit of difficulties with pointers. I'm trying to convert a string to an array of integers. The little snipet below seems to be working but I'm getting a warning :
In function 'charToInt32' warning: assignment makes pointer from integer without a cast [enabled by default]| ||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|
The warning comes from the line
int32result[pos] = returnedInteger;
So I'm trying to understand what's the best solution. Should I use strncpy (but can I use strncpy for integers?) or something else or did I just completly misunderstand pointers?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int charToInt32(char * clearText, unsigned int * int32result[])
{
int i = 0, j = 0, pos = 0; /* Counters */
int dec[4] = {24, 16, 8, 0}; /* Byte positions in an array*/
unsigned int returnedInteger = 0; /* so we can change the order*/
for (i=0; clearText[i]; i+=4) {
returnedInteger = 0;
for (j=0; j <= 3 ; j++) {
returnedInteger |= (unsigned int) (clearText[i+j] << dec[j]) ;
}
int32result[pos] = returnedInteger;
pos++;
}
return pos;
}
int main()
{
int i = 0;
unsigned int * int32result[1024] = {0};
char * clearText = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf(">>Clear: %s\n", clearText);
charToInt32(clearText, int32result); // Do the conversion to int32.
printf(">>Int32 converted: ");
for (i=0; int32result[i]; i++)
printf("%u ", (unsigned int) int32result[i]);
printf("\n");
return 0;
}
Also, at the end of the program I have the following line:
printf("%u ", (unsigned int) int32result[i])
Casting int32result[i] to unsigned int is it the only solution to avoid another warning of using %u for an unsigned int * ?
I did check the other "assignment makes integer from pointer without cast" topics/question but I could not quite get a final answer from them.
Thank-you for your help.