简体   繁体   中英

Can we use a non-pointer as a pointer in C

I'm fairly new to C but I was wondering, can we use something like an unsigned int like a void pointer. I wrote this short example to illustrate what I want to do but I can't seem to get it to work so I was wondering if it was even possible:

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);
    }

}

The above code is what I want to get working but this next example is what I want it to work as:

int main () {

int i;

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

void *void_pointer;

void_pointer = (void *) int_array;

for(i=0; i < 5; i++) {
printf("[integer pointer] has an adress of %p, and contains %d\n", void_pointer, *((int *)void_pointer));
void_pointer = (void *) ((int *)void_pointer + 1);
}

void_pointer = (void *)char_array;
for(i=0; i<5; i++) {
printf("[char pointer] has an adress of %p and contans %c\n", void_pointer, *((char *)void_pointer));
void_pointer = (void *) ((char *)void_pointer + 1);
}

}

I understand that what I'm trying to do is probably unconventional but I really want to know if I can do this. When I try to compile the first one I get an error that says this:

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));

When I run the second i get this:

[integer pointer] has an adress of 0x7fffcba7a5f0, and contains 1
[integer pointer] has an adress of 0x7fffcba7a5f4, and contains 2
[integer pointer] has an adress of 0x7fffcba7a5f8, and contains 3
[integer pointer] has an adress of 0x7fffcba7a5fc, and contains 4
[integer pointer] has an adress of 0x7fffcba7a600, and contains 5
[char pointer] has an adress of 0x7fffcba7a5e0 and contans a
[char pointer] has an adress of 0x7fffcba7a5e1 and contans b
[char pointer] has an adress of 0x7fffcba7a5e2 and contans c
[char pointer] has an adress of 0x7fffcba7a5e3 and contans d
[char pointer] has an adress of 0x7fffcba7a5e4 and contans e

After taking your responses into consideration, I changed the original code to this:

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 *) ((int *) unsigned_int + 1);
    }

    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 *)((char *) unsigned_int + sizeof(char));
    }

}

Its not exactly what I was trying to do but I guess what I wanted to do can't be done so this code serves the same output. Editing the code led me to this next small question. Why can we use:

  unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));

and when we try it with int, it jumps too many bytes at a time and gives us random numbers. To fix that issue I just changed it to:

 unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);

But why didn't it work with sizeof(int)?

In general, no. There is no guarantee that any pointer can be represented in unsigned int . The conversion from pointers to integers is implementation-defined, and might be meaningless. In practice it usually is meaningful, but you need an integer type with sufficiently many value bits to represent all pointers. uintptr_t (from stdint.h ), if defined, is such a type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM