Question:
In C, why is my character pointer,upon which an integer pointer is cast,reading 4 bytes instead of 1 bytes?
electric
2012-08-09 11:02:56 UTC
I guess I didn't put it properly in the question, so let me present my question in context of the following code.(The code compiles without error or warning).What I have done is I have cast an integer pointer "ptr_int" to a character pointer "ptr_char".Now since a character pointer when incremented by 1 points to the next byte (unlike the 5th byte as in integer pointer) I add 4 to the character pointer to access the consecutive elements of the integer array sum[].

What I want to ask you is,since "ptr_char" is a character pointer, isn't the program supposed to interpret only the SINGLE byte pointed to by the character pointer?Since we know character pointers consider 1 bytes.Why is then in my code snippet, a whole 4 bytes after the addressed pointed by the character pointer are being considered?I mean, my code outputs "35,#" , that is, for the "%d" format specifier, it reads the whole 4 bytes representing the integer instead of the first byte of the integer.Similarly, for the "%c" format specifier , it considers the ASCII value represented by the 4 bytes (ie 35) instead of the first byte alone.

Why is this happening?I mean, isn't a character pointer supposed to consider only the single byte pointed by it, just like an integer pointer is supposed to consider the 4 bytes after the address it points to?Though I am incoherent , I am sure you know what I mean to ask.So kindly answer this one.Thanks


#include

int main()
{

int sum[3]={13,25,35};
int *ptr_int=sum;
char *ptr_char;
ptr_char=(char *)ptr_int;
printf("%d , %c",*(ptr_char+8),*(ptr_char+8));

}

OUTPUT: 35,#


///Please don't ignore this question if you are Henni, Ratchetr,Husoski,Oops,MrMe,Peteams,Laurence,Jonathan............
Three answers:
2012-08-09 11:12:40 UTC
> it reads the whole 4 bytes representing the integer

No it doesn't. You just happen to be using a platform that stores integers in the little endian order.



Try the following program:



#include



int main(void)

{

        int sum[3] = { 13, 25, 35 };

        size_t n = sizeof sum;

        unsigned char *ptr;



        for (ptr = sum; n--; ptr++)

                printf("%u ", *ptr);

        puts("");

        return 0;

}



Read about byte order here: http://en.wikipedia.org/wiki/Endianness



edit:



> c|9|warning: assignment from incompatible pointer type|

> Why is it so and what to do about it?



It's safe to do. The real issue is with converting an ``unsigned char *'' to an ``int *'' (if its value is incorrectly aligned). You can quieten your compiler by using an explicit cast.



> What is "size_t n" and what it means?We haven't even defined "size_t" or " n ".



It's an unsigned integer value that is guaranteed to be able to store the size of any object. It's defined in stdio.h as well as a few other standard libraries. sizeof yields a value of this type.



> Further,aren't we supposed to write "sizeof(sum)" instead of "sizeof sum"?



No. sizeof is an operator, not a function.



> And at last, what is the difference between and "unsigned char *ptr" and "char ptr"?The last one sounds simple but is really confusing me. PLZ henni, this much only.



The former is a pointer to unsigned char, the latter is a char. If your question is about the difference between ``unsigned char'' and ``char'', then unsigned char is unsigned, and char is either signed or unsigned.
Michael
2012-08-09 11:16:57 UTC
Your code is doing what I would expect it to do.



Assuming that sizeof(int) is 4, then ptr_char+8 points to sum[2].



Assuming that your system has little endian byte order then that byte will contain the value 35 which is what your program prints out.



Try setting sum[2] to a value that doesn't fit in a single byte and you should see that your program is only accessing the low byte.



For example, if you set sum[2] to 257 (0x101) then your program should print the value 1.
?
2016-09-11 15:59:36 UTC
The right reply is 1024. In industry enterprise they have a tendency to circular it to one thousand for the reason that it sort of feels simpler. In a laptop more often than not the whole thing is founded on a energy of two. For instance two^10 is 1024. bit = one million = two^zero byte = eight =two^three bits kB = 1024 bytes = two^10 bytes MB = 1024 kB = two^10 kB GB = 1024 GB = two^10 MB TB = 1024 TB = two^10 GB


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...