I just started learning C a few days ago, and I had difficulties with pointers. I am trying to convert a string to an array of integers. The little sniper below seems to work, but I get a warning:
In the 'charToInt32' function warning: assignment makes a pointer from an integer without cast [enabled by default] | || === Assembly completed: 0 errors, 1 warning (0 minutes, 0 seconds) === |
Warning comes from line
int32result[pos] = returnedInteger;
So, I'm trying to figure out what is the best solution. Should I use strncpy (but can I use strncpy for integers?) Or something else, or did I just completely understand the wrong 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; }
In addition, at the end of the program, I have the following line:
printf("%u ", (unsigned int) int32result[i])
Bringing int32result [i] to unsigned int is the only solution to avoid another warning about using% u for unsigned int *?
I checked another βassignment makes integers from a pointer without castβ topics / questions, but I could not get a final answer from them.
Thank you for your help.