structs in C/C++

Page 1 of 1

1 Replies - 242 Views - Last Post: 25 October 2019 - 04:19 PM Rate Topic: -----

#1 Davidjohnson   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 25-October 19

structs in C/C++

Posted 25 October 2019 - 02:17 PM

Hi ;
I'm primarely coding in javascript right now but the concept of struct is the same concept as C/C++ which I already have learnt first C/C++ fundementals but didn't get deeply in its concepts.
I moved to code in javascript and then I discoverd that I need to get back one step to c/c++ and understand well the subject of structs ..

Well, my problem with struct that I understand all struct concept and its manipulation and how PC works along them, but there's still something ambiguous for me and need your help please.
Assume I have struct called BOOK with two attributes : size , font.
since I write
and its address on the memory is 0011 then implicitly is the same thing to define a variable called size with address 001? I mean with implicitly, "in aspect of understanding" we can say this understand the concept ..
am I right?

secondly, when I tell the PC
does the PC go first to BOOK address then afterward goes to size address where it's inside the book? in realy life if I have a book "assume it's on specific address" , and if I need something from what's found inside it, then I open it and lookup to specific line "specific address" to get the information about what I want .... so is that the same concept how it works with structs?!

In brief, what's confused me , once I write SOMETHINGBOOK.somethingInsideBOOK , after I write "." I get confused .... but if it's like general variable "not struct" just a variable like
 int a 
then it's fine and works fine with me .. could anyone provide me good explanation to grasp the concept of structs and accessing structs? exactly when I start dealing with points "BOOK.size" here's my confusing start ... and confusion comes because I start using "points" to access struct which it's not like a general variable .... any cooperation?
I start think on structs like that, whereever you stop your accessing into a struct then you're there and implicitly you have a variable on the address you stop at ...., am I right?

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: structs in C/C++

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon

Reputation: 7501
  • View blog
  • Posts: 15,545
  • Joined: 16-October 07

Re: structs in C/C++

Posted 25 October 2019 - 04:19 PM

View PostDavidjohnson, on 25 October 2019 - 05:17 PM, said:

I have struct called BOOK with two attributes : size , font.

Excellent. Let's define that in C.

struct BOOK {
    int size;
    int font;

View PostDavidjohnson, on 25 October 2019 - 05:17 PM, said:

since I write [li] BOOK.size [/li] and its address on the memory

Nope. Full stop. That doesn't make any sense. A struct is a "structure." It has no storage unto itself, it defines how storage organized.

In most any language you have basic value types. e.g. char, int, float. You can program using just that, but what if you have something that logically contains multiple values, like a point in space or an item in inventory?

You BOOK describes a thing, but it isn't a thing. You'd use it like so:
#include <stdio.h>
#include <stdlib.h>

struct BOOK {
    int size;
    int font;

void showBook(struct BOOK book) {
    printf("book: size=%d, font=%d\n", book.size, book.font);

int main() {
    struct BOOK book1;
    struct BOOK book2;

    book1.size = 1; book1.font = 42;
    book2.size = 2; book2.font = 69;

    return 0;

The size in memory is at least what is required to store two ints, but can be more depending on machine. In C, you can actually look into that:
struct BOOK book;
printf("book = %d\n", sizeof(book));

Indeed, if you're curious about how things float around memory, C is the way to go. Most modern languages abstract such things away from the programmer as a matter of course. C puts in it your face and forces you do deal with it. Note, that above program isn't the best C, as you should really pass a pointer and not a copy of the value. I'll leave that for you to investigate.

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1