The destination makes the pointer from the whole without cast and other questions

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.

+6
source share
4 answers
 unsigned int * int32result[1024] 

declares an array of 1024 unsigned int pointers. I think you need an array of ints instead

 unsigned int int32result[1024] 

You have a similar problem in charToInt32 , where the unsigned int * int32result[] argument specifies an array of unsigned int arrays. You have one array, so instead of unsigned int * int32result you can pass (i.e. Delete [] ).

The rest of your code should work then. Calls like

 charToInt32(clearText, int32result); 

are equivalent

 charToInt32(clearText, &int32result[0]); 
+5
source

You declare charToInt32 as argument pointer-to-pointer-to- unsigned int ,

 int charToInt32(char * clearText, unsigned int * int32result[]) 

but you use the argument as an unsigned int array, so the argument should be

 unsigned int *int32result 

or equivalent (in function declaration! No at all)

 unsigned int int32result[] 

And in main it should be

 unsigned int int32result[1024] = {0}; 

you also have one level of pointers too much.

+2
source

The warning you receive is due to the fact that you are declaring an integer value to the pointer.

 /* int32result is an array of POINTERS */ unsigned int * int32result[]; /* But returnedInteger is an int */ unsigned int returnedInteger = 0; /*...*/ /* int32result[pos] is a pointer to an integer ... * Thus here, you assign a "pure" integer to a pointer, * hence the warning */ int32result[pos] = returnedInteger; 

It is also quite reasonable that the compiler generates a warning when it gets into printf if you did not produce this value. In fact, pointers on ordinary machines are often 32 or 64 bits, which is enough to avoid loss of information during an erroneous assignment. This is why your print statement should look as if the program was working as expected.

+1
source
 unsigned int * int32result[] 

- an array of pointers unsigned int , so you assign a value to the pointer.

0
source

Source: https://habr.com/ru/post/945166/


All Articles