This question already has answers here:
C Unions output unclear
(2 answers)
Closed 6 years ago.
I was at an interview and the following code was given to me:-
union mix
{
int i;
char c[2];
};
int main()
{
union mix key;
key.i=512;
printf("\n %d,%d,%d",key.i,key.c[0],key.c[1]);
return 0;
}
and when I came home and ran it on the system I got the output:- 512,0,2.
Can anyone explain it to me how it works or why is c[1]=2?
Edit :- all I know is that it is related to bit storage in the memory.
It's a union, meaning i and c occupy the same memory.
512 is 0x200, meaning first byte is zero, second is 2 (each byte takes two hex digits).
This of course depends on the endianness of your CPU.
Related
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I can't figure out where the problem is in my program?
#include <stdio.h>
int main()
{
int a[2][2]={{1,2},{3,4}};
printf("The value of a[2][1] is %d",a[2][1]);
return(0);
}
I expected the answer to be 3, it's actually wow! 32765 wait! what!? I'm pretty confused.
Can someone help?
You don't have anything in the spot a[2][1]. I think what you meant to put is a[1][0]. Remember that the index starts at 0 not at 1.
The reason why you are getting that big number is because that number was already sitting there in that memory location. It has nothing to do with the array you created.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I wanna use int array in shared memory,after writing 1,2,3 into it,I expect read it like this:1,2,3.But I read this:3,2,1.I don't know why
write code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);
*gIn++=10;
*gIn++=20;
*gIn++=30;
sleep(10);
return 0;
}
read code:
int *gIn;
int main(){
int id;
id = shmget(0x666,1024,IPC_CREAT|0666);
gIn=(int *)shmat(id,NULL,0);
printf("%d|%d|%d\n",*gIn++,*gIn++,*gIn++);
return 0;
}
I expect the output of read process to be 10|20|30,but the actual output is 30|20|10.It's very strange.I don't know why
The problem is this line: printf("%d|%d|%d\n",*gIn++,*gIn++,*gIn++);. The order of evaluation for the parameters to printf is implementation defined. In your case it just happens to do this in an order you didn't expect.
Suggest you pull out the values separately before the printf in local variables (or an array) and then print the value.
This question already has answers here:
Why does malloc initialize the values to 0 in gcc?
(10 answers)
Closed 5 years ago.
I was implementing a program related to the use of dynamic allocation in C.
Testing the same piece of code on Visual Studio 2017 and on other IDEs (Dev C ++, Codeblocks, etc.) I have different behaviors:
size_t newDim = 9;
char *p = malloc((newDim + 1) * sizeof(char));
p[newDim] = '\0';
printf("%d\n", strlen(p));
The output of printf() on Visual studio is: 9
other IDEs: 3 sometimes 4.
But when I fill the array with dim-1 characters, the same printf() produces a correct output on the other IDEs. I think that the different compilers have a different way of managing the allocated memory, could someone explain the problem in more detail?
Thank you
malloc is not initializing the memory allocated, so the allocated space might have zeros in arbitrary places giving different string lengths.
This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 5 years ago.
Here is my code:
#include<stdio.h>
int main()
{
int i, list[1];
list[0]=1;
list[1]=2;
list[2]=7;
list[55]=70;
i=sizeof list;
printf("%d %d %d %d %d Size of array is %d",list[0],list[1],list[2],list[3],list[55],i);
return(0);
}
It returns "1 2 7 4 70 Size of array is 4". Why can i assign, say 55 to list[55]. list[55] should not exist as I only gave the array list enough memory for 1 integer, right? In addition shouldn't this give me an error as list[3] doesn't exist? and if for some reason i am changing the size of the array why isn't the size 56? It comes out as 4.
So what is happening to give me the output i got?<--{main question}
[As i don't want to create a separate thread for a related question, why when i code int list[0]; the program crashes, if i am somehow changing the size from 1 to 4 shouldn't I be able to change the size from 0 to 4?]
Thanks for your help, I know this probably a stupid or obvious question.
This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
What does ': number' after a struct field mean? [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to understand the following code in C:
struct values{
int i:3;
int j:3;
int k:2;
};
int main(){
struct values v = {-6,2,5};
printf("%d %d %d", v.i,v.j,v.k);
}
This code produces the following output:
2 2 1
I'm trying to understand what does it mean the assignment for int values used inside the struct i.e.: int i:3 ?
I know that : is not an operator. So what does it do?
Also, can someone explain how this output is achieved?
Thanks very much!
The numbers specifies the length in bits for each field.
Hence i and j are represented in 3 bits while k in 2 bits.
By the way this is question is clearly a duplicate of this question and there's a very good answer I suggest you to read.