954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

question on type conversion in c

i found this code in a book. i am finding it confusing.

#include<stdio.h>
int main()
{
int arr[3]={2,3,4};
char *p;
p = (char*)arr;         // line A
p = (char*)((int*)p);
printf("%d",*p);
p= (char*)(p+1);        //line B
printf("%d\n",*p);
return 0;
}

the answer is: 2 0
but i cant figure out how/why that '0' gets printed
in line A does 'p' point to 2 or a garbage value?? (since p is a character pointer)
and what is line B doing??

student_learner
Newbie Poster
12 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

in line B, when the pointer is incremented by 1, why is it not printing the next element of the array, i.e. 3?? :|

student_learner
Newbie Poster
12 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

p is a pointer to char, not int. So when you increment by 1, it increments by 1 byte instead of the number of bytes in an int. I think you should throw that book away if that program is a sample of its quality.

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 
p is a pointer to char, not int. So when you increment by 1, it increments by 1 byte instead of the number of bytes in an int. I think you should throw that book away if that program is a sample of its quality.

thats what i thought , but why is it printing 0, but not a garbage value?

student_learner
Newbie Poster
12 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

A char tends to be 8 bits.
An int on a 32-bit machine is 32-bits.
If an int contains a 2, it actually contains {2,0,0,0} (in a 32-bit system)
Try this code:

#include<stdio.h>

int main()
{
	unsigned int i, j;
	int arr[3]={2,3,4};
	char *p;
//	p = (char*)arr;         // line A
	for(i=0;i<3;i++){
		printf("Integer:%d\n",arr[i]);

		for(j=0;j<sizeof(int);j++){	//I'm using sizeof(int) to get the exact size of int. I can't assume it's 4.
			p=(char*)&arr[i];	//point p at arr
			p+=j;			//move p to current byte
			printf("Byte[%d]:%d\n",j,*p);
		}
		printf("*********\n");
	}
	return 0;
}

Why do I not know the size of int?
An int is usually the size of the number of bits in the system it's compiled for.
This can be 16, 32, or 64.
Note that just because I run a program on a 64-bit system, doesn't mean I'm getting 64-bits. If it's a 32-bit program, it's a 32-bit number.

DeanMSands3
Junior Poster
185 posts since Jan 2012
Reputation Points: 37
Solved Threads: 26
 

A char tends to be 8 bits. An int on a 32-bit machine is 32-bits. If an int contains a 2, it actually contains {2,0,0,0} (in a 32-bit system) Try this code:

#include<stdio.h>

int main()
{
	unsigned int i, j;
	int arr[3]={2,3,4};
	char *p;
//	p = (char*)arr;         // line A
	for(i=0;i<3;i++){
		printf("Integer:%d\n",arr[i]);

		for(j=0;j<sizeof(int);j++){	//I'm using sizeof(int) to get the exact size of int. I can't assume it's 4.
			p=(char*)&arr[i];	//point p at arr
			p+=j;			//move p to current byte
			printf("Byte[%d]:%d\n",j,*p);
		}
		printf("*********\n");
	}
	return 0;
}

Why do I not know the size of int? An int is usually the size of the number of bits in the system it's compiled for. This can be 16, 32, or 64. Note that just because I run a program on a 64-bit system, doesn't mean I'm getting 64-bits. If it's a 32-bit program, it's a 32-bit number.

oh, i am sorry, here in my query int is of 2 bytes.
i am running it on a turbo c compiler. its of 16 bits.
which mean that it would contain {2,0} right?

By the way thanks a lot!! you are the dude! :D
this is what i didn't know:

"If an int contains a 2, it actually contains {2,0,0,0} (in a 32-bit system)"

and it cleared my concept. :)

student_learner
Newbie Poster
12 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Fantastic. I'm glad to have helped.

DeanMSands3
Junior Poster
185 posts since Jan 2012
Reputation Points: 37
Solved Threads: 26
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You