LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Typecasting non-pointers in C (https://www.linuxquestions.org/questions/programming-9/typecasting-non-pointers-in-c-4175557128/)

yasgur99 10-25-2015 01:52 PM

Typecasting non-pointers in C
 
I'm rather new to programming and I know how to typecast pointers but I was having a little trouble typecasting non-pointers. Here is a sample piece of code which I think illustrates my question well.
Code:

#include <stdio.h>

int main () {

        int i;

        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1, 2, 3, 4, 5};

        unsigned int unsigned_int;

        unsigned_int = (unsigned int) int_array;

        for(i=0; i < 5; i++) {
        printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
        unsigned_int = unsigned_int + sizeof(int);

        }

        unsigned_int = (unsigned int) char_array;

        for(i=0; i<5 ; i++) {
                printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
                unsigned_int = unsigned_int + sizeof(char);
        }

}

When I run this code, I would expect that the unsigned_int would be able to act as a void pointer would. I understand that in reality one would just change it into a void pointer or just create a char and an int pointer but I am trying to understand typecasting fully and I wanted this to work. This is the warnings I get when I try to compile.

Code:

pointer_type5.c: In function ‘main’:
pointer_type5.c:12:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
        unsigned_int = (unsigned int) int_array;
                        ^
pointer_type5.c:15:92: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
        printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
                                                                                            ^
pointer_type5.c:20:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
        unsigned_int = (unsigned int) char_array;
                        ^
pointer_type5.c:23:103: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
                printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));

Just making sure I'm clear, I want to know how to typecast this unsigned int so the program will work.

NevemTeve 10-25-2015 02:41 PM

There is an integer type called intptr_t defined in inttypes.h that can be safely used to hold a pointer (also there is an unsigned uintptr_t) type.

yasgur99 10-25-2015 03:59 PM

Quote:

Originally Posted by NevemTeve (Post 5440062)
There is an integer type called intptr_t defined in inttypes.h that can be safely used to hold a pointer (also there is an unsigned uintptr_t) type.

I don't think that is what I want to do. I want to typecast a nonpointer into a pointer.

ntubski 10-25-2015 05:18 PM

Quote:

Originally Posted by yasgur99 (Post 5440096)
I don't think that is what I want to do. I want to typecast a nonpointer into a pointer.

An intptr_t is a nonpointer. It's simply defined to be the same size as a pointer.

rtmistler 10-26-2015 02:33 PM

The correct thing to do was instead:
Code:

        int int_array[5] = {1, 2, 3, 4, 5};

        int *array_pointer;

        array_pointer = int_array;        // one method
        array_pointer = &int_array[0];    // another method

And then to increment the pointer, you just can do:
Code:

        array_pointer++;
And that will point to the next array element. To get the address being pointed to you'd use something like:
Code:

        printf("Address of element is: %p\n", array_pointer);
But why bother? Memory locations in a general system are moved around a lot. All you can really benefit by is seeing that you increment by the integer size (machine dependent) as you step through the array. You're better off validating all that through a debugger like GDB.

Unless you need to, I would not have a pointer to an integer pointing to a character array. I've had instances where I need to convert a character to an integer, so what I'd do is something like this:
Code:

    char array_of_char[5] = { 'a', 'b', 'c', 'd', 'e' };
    char *pArray = array_of_char;
    int converted_value;

    converted_value = (int)(*pArray);

If you're just experimenting, I'd really recommend GDB so that you can view variables and inspect things to understand the flow of your program. Set and breakpoint, hit it, step through the program, and inspect variables and memory as you go to validate things. Meanwhile if you're trying to do something specific, then indicate what you're trying to do so people can offer some better advice.

genss 10-27-2015 07:12 PM

just to add

do you want to do type punning ?


All times are GMT -5. The time now is 11:17 AM.