Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment makes pointer from integer without a cast and other questions

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.

like image 487
Indigo Avatar asked May 16 '13 12:05

Indigo


3 Answers

unsigned int * int32result[1024]

declares an array of 1024 unsigned int pointers. I think you want an array of ints here 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 a single array so could pass unsigned int * int32result instead (i.e. remove the []).

The rest of your code should just work then. Calls like

charToInt32(clearText, int32result);

are equivalent to

charToInt32(clearText, &int32result[0]);
like image 121
simonc Avatar answered Oct 18 '22 17:10

simonc


You declare charToInt32 to take a pointer-to-pointer-to-unsigned int as an argument,

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

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

unsigned int *int32result

or equivalently (in a function declaration!! Not in general)

unsigned int int32result[]

And in main, it should be

unsigned int int32result[1024] = {0};

there you also have one level of pointers too many.

like image 30
Daniel Fischer Avatar answered Oct 18 '22 18:10

Daniel Fischer


The warning you get comes from the fact that you hare assigning an integer value to a 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 totally sane that the compiler raises a warning when hitting the printf if you do not cast the value. As a matter of fact, pointers on common machines are often 32 or 64 bits, which is enough to avoid information loss during the faulty assignment. That is why your print statement should look like the program works as expected.

like image 1
Rerito Avatar answered Oct 18 '22 17:10

Rerito