1

Hi how to access character array using integer point.

char arr[10] = {'1','2','3','4','5','6','7','8','9','10'};
int *ptr;

How i can print values of 'arr' using pointer ptr?

davejagoda
  • 2,420
  • 1
  • 20
  • 27
user1808215
  • 273
  • 1
  • 6
  • 11
  • 2
    If you try to access an array of `char`s using an pointer to `int`, your program will exhibit undefined behavior. Why do you want to use a pointer to `int` instead of a pointer to `char`? – R Sahu Nov 23 '15 at 04:31
  • Possible duplicate of [using pointers to display content of array](http://stackoverflow.com/questions/6814533/using-pointers-to-display-content-of-array) – Tot Zam Nov 23 '15 at 04:31
  • `int *ptr = 0; for (int i = 0; i < 10; ++i) printf("%d\n", arr[ptr,i]);` – M.M Nov 23 '15 at 05:08
  • 1
    your char array should give an error '10' is not a single character – Kishan Kumar Nov 23 '15 at 05:17
  • @KishanKumar the behaviour of `'10'` is implementation-defined, but it should not give an error – M.M Nov 23 '15 at 05:21
  • 1
    It won't give an error, but will issue a warning `multi-character character constant; overflow in implicit constant conversion` – niyasc Nov 23 '15 at 05:25
  • sorry my bad. But guys can you just check this out. http://ideone.com/GrrKNF and point out my error.its related to this question – Kishan Kumar Nov 23 '15 at 05:27
  • You don't do that. It makes no sense. What is your real problem? – n. m. could be an AI Nov 23 '15 at 05:27
  • @M.M `'10'` is impl defined, but here it goes into a `char` array, so it will get truncated before the array can be accessed. The result will probably be `'0'` just like if you said 0x3130 instead – Craig Estey Nov 23 '15 at 05:35
  • @user1808215 why do you need this, maybe if you explain what you are trying to solve then people can give better answers. – AndersK Nov 23 '15 at 05:52
  • 1
    @KishanKumar ideone is not very good. `int *ptr = arr;` is illegal, whatever happens after that is some artifact and not a part of Standard C – M.M Nov 23 '15 at 05:54

3 Answers3

3

It is a little unclear what your goal is, but trying to print out a character array with an integer pointer is a bit like trying to get to the second step taking four-steps at a time. When you tell the compiler that you would like to reference a memory address with an integer pointer, the compiler knows that an integer is sizeof (int) bytes (generally 4-bytes on x86/x86_64). So attempting to access each element in a character array with an integer pointer and normal pointer arithmetic wouldn't work. (you would be advancing 4-bytes at a time).

However printing the character array though an integer pointer is possible if you use the integer pointer for the starting address of the array and advance the pointer by the number of characters in the array by casting back to char. While it is doubtful this is your goal, the plain statement of your question seems to suggest it. To accomplish this, you could:

#include <stdio.h>

int main (void)
{
    char arr[] = {'1','2','3','4','5','6','7','8','9'};
    int *ptr = (int *)arr;
    unsigned int i;

    for (i = 0; i < sizeof arr; i++)
        printf (" %c", (*(char *)ptr + i));
    putchar ('\n');

    return 0;
}

Output

$ ./bin/char_array_int_ptr
 1 2 3 4 5 6 7 8 9

Note: your original initialization of your array with a character '10' was invalid. If this was an assignment, it is likely intended to expose you to how pointer arithmetic is influenced by type and the ability to cast from and to type char (without violating strict aliasing rules)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

If you are just after the integer values you can print out the characters as integers

for (i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
{
  printf( "%d ", arr[i] );
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
1

Using a pointer of the wrong data type to access anything is undefined behavior, thus making it not something you want to do. If you want to cast a char to an integer, you can do that. If you want to print the integer value of a char, you can do that too.

But using a pointer type integer to access a char array is undefined behavior.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • You have to be somewhat careful with the broad statements about undefined behavior. Casting to/from `char` and the *strict-aliasing rule* have a few caveats, see [**What is the strict aliasing rule?**](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule), but for a general proposition as you have written -- attempting to access a char array with an int pointer is undefined behavior. – David C. Rankin Nov 23 '15 at 08:40
  • @DavidC.Rankin feel free to eidt a caveat into my answer, if you find it appropiate. – Magisch Nov 23 '15 at 08:42
  • No, no, I think the general answer is fine, but you just don't want to give the impression that casting to from different types is always *undefined behavior*. – David C. Rankin Nov 23 '15 at 08:43