1

In the below code,

#include<stdio.h>
int main(){
  char array[] = {'1', 2, 5.2};
  char* my_pointer = array[2];
  printf("%c", *my_pointer);
}

5.2 is stored in IEEE 754 representation in memory, char picks 8 bits(first) from this float representation, due to little endian format.

C is a loosely typed language. Am allowed to cast float to char.

Why the program is core dumped?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Which is the question? – meJustAndrew Oct 10 '16 at 19:24
  • 4
    Do things that are undefined behavior, expect undefined behavior to happen. – David Hoelzer Oct 10 '16 at 19:25
  • @DavidHoelzer I know, how `5.2` is stored in memory, am picking 8 bits from it. Is that undefined? C is loosely typed language. I am allowed to cast `float` to `char`. Why should the executable core? – overexchange Oct 10 '16 at 19:26
  • It shouldn't even compile the array definition. You shouldn't been able to use a double to init the array of char. – Heisenbug Oct 10 '16 at 19:29
  • @Heisenbug Why it shouldn't compile? Because array is homogenuos data model? – overexchange Oct 10 '16 at 19:30
  • 1
    @CherubimAnand That was a mistake from me. Program is working now. – overexchange Oct 10 '16 at 19:31
  • Firstly, "char picks 8 bits(first) from this float representation, due to little endian format" - that does not make any sense. `5.2` will simply get converted to `char`, which does not have anything to do with endianness of the representation. Secondly, there's not a single `float` in your code. `5.2` is `double`, not `float`. Thirdly, `char* my_pointer = array[2];` is invalid C code. – AnT stands with Russia Oct 10 '16 at 19:34
  • @AnT What does it mean? when you say `5.2` will get converted to `char`? in memory? – overexchange Oct 10 '16 at 19:37
  • 1
    @overexchange: It has nothing to do with memory representation. When you convert `5.2` to `char` type, the compiler will generate code that loads `5.2`, truncates the value and stores it in your `char` variable as `5`. In general case, it will happen in CPU/FPU, not "in memory". – AnT stands with Russia Oct 10 '16 at 19:38
  • @AnT I got your answer – overexchange Oct 10 '16 at 19:39
  • 1
    Both `char` and `double` are arithmetic types. Conversion from one arithmetic type to another is defined in terms of values, not representations. The `double` value `5.2` is implicitly converted (not "cast") to type `char`, yielding the value `5`, or equivalently `'\x05` or `\005`. It's the character with the value `5`, often referred to (on most systems) as Control-E. Yes, you can convert a floating-point value to `char`, either explicitly (with a cast) or implicitly, and the result is well defined if the value is in range. It rarely makes sense to do so. – Keith Thompson Oct 10 '16 at 19:41
  • 1
    To clarify my previous comment, there is no such thing as an "implicit cast". A cast is an explicit conversion operator, written as a type name in parentheses. A conversion can be explicit or implicit; an explicit conversion is called a "cast". – Keith Thompson Oct 10 '16 at 19:43
  • @KeithThompson For your point: *Conversion from one arithmetic type to another is defined in terms of values, not representations*, `char string[] = {1, 2, 3};` In this syntax, I see that conversion is in terms of representation. Here binary representation of `1`(i.e., `0000 0001`)is stored in `string[0]`. Do you mean, `char` is not arithmetic type in C? – overexchange Oct 10 '16 at 20:01
  • No, the behavior of arithmetic conversions is defined in terms of the numeric values, not their representations. Of course the value stored has a representation; that's not the point. The *value* `1`, which is of type `int`, is implicitly converted to `char`, yielding the same numeric value. – Keith Thompson Oct 10 '16 at 20:03
  • @KeithThompson You mean, if I say, `printf("%d", string[0])` would give same numeric value `1`? – overexchange Oct 10 '16 at 20:05
  • You seem to be missing something, but I don't know what. Yes, `printf("%d", string[0])` will print the numeric value -- but that's because the `char` value of `string[0]` is *promoted* to `int`, because `printf` is a variadic function. – Keith Thompson Oct 10 '16 at 20:07
  • @KeithThompson For your point : *The value 1, which is of type `int`, is implicitly converted to `char`, yielding the same numeric value.*, I did not get you, when you say, *yielding the same numeric value*. `string[0]` now has value `SOH` whose internal rep is `0000 0001` – overexchange Oct 10 '16 at 20:14
  • Forget about representation. The *value* of `string[0]` is `1`. (When interpreted as an ASCII character, that happens to be SOH.) – Keith Thompson Oct 10 '16 at 20:16
  • @KeithThompson How do I retrieve the value of `string[0]`, `1` in C code? – overexchange Oct 10 '16 at 20:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125372/discussion-between-keith-thompson-and-overexchange). – Keith Thompson Oct 10 '16 at 20:22

2 Answers2

1

In your program change char *my_pointer = array[2]; to char *my_pointer = &array[2]; as pointer should store the address.

#include<stdio.h>
int main(){
  char array[] = {'1', 2, 45.2};
  char *my_pointer = &array[2];
  printf("%c", *my_pointer);
}

output:

- //NOTE: asci value of - is 45

as @AnT has mentioned in comments, when you convert 45.2 to char type, the compiler will generate code that loads 45.2, truncates the value and stores it in your char variable as 45, so when you print you get - as output.

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
  • Do you have any issues, if I have multiple data types in an array? As we say array is homogenous datamodel – overexchange Oct 10 '16 at 19:32
  • to be honest @overexchange I don't know as I never usually use arrays that way. I stick to defined behaviour. You'd be better off asking others :) – Cherubim Oct 10 '16 at 19:38
  • "array is homogenous datamodel" what the heck does that mean? In C the array has the type you defined it with. – Weather Vane Oct 10 '16 at 19:41
  • I think he meant that it stores collection of homogeneous type of data @WeatherVane – Cherubim Oct 10 '16 at 19:43
  • OP states that C is loosely typed. [Please read this](http://stackoverflow.com/questions/430182/is-c-strongly-typed). A `char[]` array stores... `char` values. – Weather Vane Oct 10 '16 at 19:44
1
char* my_pointer = array[2];

is wrong. The RHS of that is of type char, not a char*. Turning up the warning level in your compiler will help you identify problems like these.

With gcc -Wall, I get the following warning:

soc.c: In function ‘main’:
soc.c:4:23: warning: initialization makes pointer from integer without a cast
    char* my_pointer = array[2];

After that, the program has undefined behavior.

What you need is:

char* my_pointer = &array[2];

or

char* my_pointer = array + 2;
R Sahu
  • 204,454
  • 14
  • 159
  • 270