1

I'm new to c. Just learning. What i am trying to do is initialize a variable type of unsigned int and then store address of an char type array in it and after that I am trying to use int as a pointer and print the array using the unsigned int but I'am getting segmentation fault, I don't know why? It's printing value of memory address of char type array. But not the actual values at memory addresses. Can someone please help?

#include <stdio.h>

int main()
{
    char char_arr[4] = {'a', 'b', 'c', 'd'};
    int i;

    unsigned int hackyPointer;
    hackyPointer = (unsigned int) char_arr;

    for (i = 0; i < 4; i++)
    {
        printf("[hacky Pointer] now points to %p which contains value %c\n",hackyPointer, *((char *) hackyPointer));
        hackyPointer += sizeof(char);
    }

}
10
  • 3
    Type casting can't cause segfault. Please provide minimal reproducible example. Commented Aug 21, 2019 at 15:39
  • 2
    When size of address is not the same as sizeof int, then there will be an error in run-time. And if you turn warnings of your compiler on, it will tell you. Commented Aug 21, 2019 at 15:40
  • I have added source.. Please check again.. I'm using sizeof.. Commented Aug 21, 2019 at 15:43
  • 1
    One obvious issue that can cause undefined behavior is that you are using %p to print an integer. The other is that your pointer size might be larger than can fit into unsigned int. Commented Aug 21, 2019 at 15:46
  • 1
    Why are you trying to store pointer values in unsigned integer variables? Why not store pointer values in pointer variables? You're bound to have less trouble... Commented Aug 21, 2019 at 15:58

2 Answers 2

2

The problem is that you are trying to fit a bigger number than int can store, that is if you are running x64.

In 64 bit systems, pointers have a size of 8 bytes and int is usually 4 bytes. Pointer addresses work like numbers so when you cast the pointer to int, the address will be truncated.

Casting the int back to a pointer it will now contain some other address that does not belong to your program which can result in a access violation, or in other words, a segmentation fault.

Use a bigger int to hold the address, cast it to an int pointer or use uintptr_t type which is guaranteed to be big enough to hold the address.

Sign up to request clarification or add additional context in comments.

Comments

1

What you are trying to do is the following

#include <stdio.h>
#include <stdint.h>

int main()
{
    char char_arr[4] = {'a', 'b', 'c', 'd'};
    int i;

    uintptr_t hackyPointer;
    hackyPointer = (uintptr_t) char_arr;

        for (i = 0; i < 4; i++)
        {
            printf("[hacky Pointer] now points to %p which contains value %c\n",( void * )hackyPointer, *((char *) hackyPointer));
            hackyPointer += sizeof(char);
        }
}        

The program output is

[hacky Pointer] now points to 0x7ffff86d4224 which contains value a
[hacky Pointer] now points to 0x7ffff86d4225 which contains value b
[hacky Pointer] now points to 0x7ffff86d4226 which contains value c
[hacky Pointer] now points to 0x7ffff86d4227 which contains value d

You have to use an integer type that is able to contain values of pointers.

1 Comment

Thanks for your great suggestion.. I got it already it worked with unsigned long int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.