10 Replies - 565 Views - Last Post: 03 January 2019 - 09:14 AM Rate Topic: -----

#1 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

structs in C

Posted 02 January 2019 - 04:00 PM

Hi guys, I've read over the internet about structs in C and what's confusing me is:
lets assume I have a struct like this:
struct Book 
{
int size;
char name[20];
}
Book b1;


what's actually the address of b1 look/visualize in the memory? b1 is a struct with properties of name and size ..
is it like this:(assuming the address of b1 is 1234)
1234: b1
   5678: name
   5699: size 


?
I'm asking like this question because for example in type of int once we write int x, the first byte's address of 4 bytes(because it's int) is assigned to x, meaning lets assume the addresses of 4 bytes of int like this: 1 2 3 4 which all storing the data of x, so the address of x is 1 because it's by default the pc assigning the first address of the first block of 4 bytes to x.

what about the address of b1? is the pc assigning it to the address of the first property that we build in the struct(in my case &b1 is the same address of size) ? thanks !!

This post has been edited by RyanMco: 02 January 2019 - 04:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: structs in C

#2 jimblumberg   User is offline

  • member icon

Reputation: 5645
  • View blog
  • Posts: 17,347
  • Joined: 25-December 09

Re: structs in C

Posted 02 January 2019 - 04:36 PM

*
POPULAR

Quote

what's actually the address of b1 look/visualize in the memory? b1 is a struct with properties of name and size ..
is it like this:(assuming the address of b1 is 1234)

I suggest you write a program that actually illustrates the question. Writing a small complete program to test your assumptions will probably teach you more than someone answering the question.

So, I suggest you post a small complete program that properly defines the structure, then create an instance of that structure, and then lastly prints out the addresses of the various elements of the structure.

I'll even be nice to you and give you the printf() call, since printing pointers can be tricky in C.

    printf("Address of the structure: %p\nAddress of size: %p\nAddress of my_string: %p",
           (void*)&my_test, (void*)&(my_test.size), (void*)&(my_test.my_string));


By the way the structure contains two variables an int named size and a string named my_string.
Was This Post Helpful? 7
  • +
  • -

#3 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

Re: structs in C

Posted 02 January 2019 - 04:46 PM

But sir you may didnt understand my question; all i need to know what's that pointer of the struct? the pc will give me a specific pointer like 0111; how does i know what it's about?!!! And how does i know how i visualize that pointer?!
My question really dont need a program; I asked what's that pointer of the struct about and how I should visualize it?

This post has been edited by RyanMco: 02 January 2019 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: structs in C

Posted 02 January 2019 - 05:52 PM

In general, if you're trying to understand how the machine thinks about the world, the best way to do that is to write some code that explores that. That way you can construct your own visualization, and test it against the reality, until you're satisfied.

Really, asking "what does this look like in memory" is a little hard to answer, since it really doesn't look like anything in memory. Memory doesn't look like much of anything. What you probably mean is, what's the right way to visualise the structure that I'm creating here - and the right way is the one that best helps you understand what's happening.
Was This Post Helpful? 3
  • +
  • -

#5 unixplumber   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 2
  • Joined: 02-January 19

Re: structs in C

Posted 02 January 2019 - 06:12 PM

Just to be pedantic, the code in the OP is not valid C (but it is valid C++). To make it valid C you need to make one of two changes (or a variation of one of these changes):

struct Book 
{
int size;
char name[20];
}
struct Book b1;


or

struct Book 
{
int size;
char name[20];
}
typedef struct Book Book;
Book b1;

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5645
  • View blog
  • Posts: 17,347
  • Joined: 25-December 09

Re: structs in C

Posted 02 January 2019 - 06:50 PM

*
POPULAR

Quote

the code in the OP is not valid C (but it is valid C++).

By the way your code is still not valid C or C++ code either since you forgot to terminate the structure definition. Please be very careful answering questions from this user, make sure your suggestions are extremely sound and well tested. The user has yet to provide any real programs to illustrate his problems, just a bunch of "random" thoughts that in many ways are meaningless.

Quote

But sir you may didnt understand my question;

Oh really? I understood your question, my answer is for you to do some actual experimentation to try to answer your own question. You're not learning anything on your present course so you need to change something. My suggestion is that you start to actually write programs to test your assumptions. Once you actually start to do this and you don't understand why your program is outputting what is does then ask questions specific to the code you provide.

Until you provide the requested information there is really not much anyone here can do to help you. So get to work and actually write the program I suggested, then maybe we can talk.

Jim
Was This Post Helpful? 5
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: structs in C

Posted 02 January 2019 - 10:20 PM

Just to kind of emphasize this point, since it's important: you only ever learn anything about code by writing code. Period, full stop, no exceptions.
Everything else - books, teachers, help sites, etc., exists to tell you what code you should write and maybe to give you some idea of what questions you should ask about that code. You don't learn anything until you write the code.

Ah, and video tutorials? They exist only to fool you into thinking you've learned something which you haven't. Avoid them at all costs.
Was This Post Helpful? 2
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6664
  • View blog
  • Posts: 22,744
  • Joined: 05-May 12

Re: structs in C

Posted 03 January 2019 - 08:40 AM

Slightly off topic: Professional gunsmiths love all the "how to modify your gun with just a Dremel tool" YouTube videos. Instead of taking away business, it actually generates a lot of business from folks needing repairs after they screw up.
Was This Post Helpful? 2
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6664
  • View blog
  • Posts: 22,744
  • Joined: 05-May 12

Re: structs in C

Posted 03 January 2019 - 08:45 AM

Back on topic: The point of the Youtube video meander above, is that it takes hands on experience to actually learn to program. Part of learning is trying things for yourself and little experiments to see what happens when you change things within a program, or try different designs/approaches.
Was This Post Helpful? 2
  • +
  • -

#10 unixplumber   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 2
  • Joined: 02-January 19

Re: structs in C

Posted 03 January 2019 - 09:11 AM

View Postjimblumberg, on 02 January 2019 - 06:50 PM, said:

By the way your code is still not valid C or C++ code either since you forgot to terminate the structure definition.


Aah! What a rookie mistake! I didn't even notice the code in the OP was missing semicolons. Good catch.

But you're right about this guy. He posts a bunch of random stuff without seeming to understand any of it (I just read through the 5-page thread on cprogramming.com about NULL from 2015), making assumption after assumption without bothering to test any of his theories, and instead writes incoherent posts with code that doesn't even compile. You'd think that, after 3 years, someone would learn basic syntax and would be capable of compiling code to see if it's valid. You'd also think that they'd understand some of the basic concepts like structures and "for" loops. But no! They seem allergic to learning or reading or understanding anything we write.
Was This Post Helpful? 3
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14687
  • View blog
  • Posts: 58,690
  • Joined: 12-June 08

Re: structs in C

Posted 03 January 2019 - 09:14 AM

The plumber gets it!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1