2

I'm studying section 2.1 from Computer Systems A Programmer's Perspective and I understand what the code is doing but I don't understand why there are parenthesis around the formal parameter "byte_pointer" followed by &x?

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start, int len) {
    int i;
    for (i = 0; i < len; i++)
        printf(" %.2x", start[i]);
    printf("\n");
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int));
}

void show_pointer(void *x) {
    show_bytes((byte_pointer) &x, sizeof(void *));
}
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
David
  • 59
  • 6
  • 6
    That is called casting. Google type-cast. – Sourav Ghosh Jan 16 '18 at 08:28
  • 2
    `byte_pointer` is not a formal parameter (at call point you only have effective parameters). 1) `byte_pointer` is a type, 2) expression like `(type)value` is a cast, a conversion if you prefer. Thus address of `float x` is converted to address of *byte*. – Jean-Baptiste Yunès Jan 16 '18 at 08:30
  • 1
    And if you don't have a grasp on C concepts yet, I think you need a slightly more [basic book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Jan 16 '18 at 08:30
  • @StoryTeller C++ has its own casts and learning C is not by any means a prerequisite. – Aluan Haddad Jan 16 '18 at 08:31
  • @AluanHaddad: I'd still advise it though. Learn the low-level stuff via C. – Bathsheba Jan 16 '18 at 08:31
  • @AluanHaddad - I don't see any C++ in this post. And I agree with Christian's removal of the C++ tag. This is C, nothing wrong with it. And it's best to learn it without conflating it with C++. – StoryTeller - Unslander Monica Jan 16 '18 at 08:33
  • Well, after wasting years in university in applied C++ courses taught by professors who never moved beyond C, I disagree as it often leads to stagnation and misconception. – Aluan Haddad Jan 16 '18 at 08:33
  • @AluanHaddad - I don't get what you are disagreeing with, exactly. That one should learn C if they intend to work in C? – StoryTeller - Unslander Monica Jan 16 '18 at 08:35
  • I'm working my way through a community college CS sequence and we never took any C classes, only C++. I will try to learn C on my own, but now this class followed my C++ sequence. So if I understand correctly is (byte_pointer) casting the formal parameter to 1 byte? Sorry if I'm asking noob questions, but I've been searching endlessly on google. – David Jan 16 '18 at 08:36
  • @StoryTeller I apologize for being vague. I disagree with the sentiment that you should learn C first, or the low level parts in C, if you are studying C++ – Aluan Haddad Jan 16 '18 at 08:37
  • 1
    @AluanHaddad - I disagree with that too (heck, my profile even contains a link to [this fantastic lecture](https://www.youtube.com/watch?v=YnWhqhNdYyk)). I never suggested it. – StoryTeller - Unslander Monica Jan 16 '18 at 08:38
  • 1
    @StoryTeller Ah, OK. It was my misunderstanding then. I've seen that lecture and it is indeed fantastic! Thanks for linking it. – Aluan Haddad Jan 16 '18 at 08:40
  • A book that tells you to hide pointers behind a typedef is a very bad one. It is cold outside, light a fire and put that book to some good use! – Lundin Jan 16 '18 at 10:52

1 Answers1

5

You are typecasting a float pointer to an unsigned char*. And then you will access each of the float variable's bytes. As pointer arithmetic is dictated by the type of thing it points to - the increment or dcrement on it will be directbed by sizeof(unsigned char) or 1 byte. You will access the float byte by byte. By casting we are basically saying that you need to consider this as unsigned char* even if it the float* that we are passing. There is a change in type information - and we are making the compiler aware that we are aware of it by providing the casting.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • So if I understand correctly is (byte_pointer) casting the formal parameter to 1 byte? – David Jan 16 '18 at 08:40
  • @David.: casting to `unsigned char*` and as `sizeof(unsigned char)` is `1` yes now it accesses byte by byte – user2736738 Jan 16 '18 at 08:41
  • I'm using VS 2015 and I removed the type casting and only included &x, and it still printed each byte. Did the compiler type cast the parameter? – David Jan 16 '18 at 08:46
  • 1
    No, the cast does not cast anything to 1 byte. It does not cast something to `char`. It casts a pointer to `float` into a **pointer to `char`**. While `char` is 1 byte, pointer to `char` is not 1 byte. – Gerhardh Jan 16 '18 at 08:53