Thread: Can someone please help me with a data structure question?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    44

    Can someone please help me with a data structure question?

    Can someone please tell me how to point a data structure that I have created to a particular memory address?
    TIA.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It sounds like you need a good Pointers in C - Tutorial - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    44

    I unfortunately didn't get my answer there

    Quote Originally Posted by quzah View Post
    It sounds like you need a good Pointers in C - Tutorial - Cprogramming.com


    Quzah.
    I unfortunately didn't get my answer there, can you please explain it to me? Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like you need to read it again. If you understand pointers, then you understand how to point anywhere you feel like. If you don't understand pointers, then you need to go and actually read it this time.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If that tutorial doesn't explain it well enough, Google will find dozens, if not hundreds of other ones to read. Do you have a textbook or class notes you can review? This one is a little longer and more in depth, but good: Eternally Confuzzled - All About Pointers.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    I am still not finding out how you can set the address to point a structure too. I see plently of examples online for pointing an integer type variable to address but not a structure type variable.
    Is this possible:

    Code:
     
    typedef struct {
     Summary_Type SummaryBlock;
     Log_Type FaultLog[10];
    } Result_Type;
    
    Result_Type *ptr_Results = (* Result_Type) 0x1F700000;
    I am trying to get ptr_Results to point to address 0x1F700000. Please let me know if this correct.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assuming your OS actually lets you do that, yes.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, that's correct. A pointer contains an address. The type of that pointer (int, struct, etc) just tells the compiler what kind of data it should expect there, so the int pointer examples translate very easily to your struct pointer examples. Note that assigning pointers to literal addresses is rarely a good idea unless you're in some embedded system or such, where, e.g., you have direct access to memory mapped IO.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    This didn't compile... It didn't work for the struct data type.
    I have similar examples in my code where it worked for a int.

    int *mem = (int*) START_ADDR;

    but it didn't work for the struct. Any more hints please?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, because your type cast is backwards. Try (Result_Type *) 0x17F00000.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    Thank you!! That solved it..

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    One more question please, can you please tell me how I would set 1 bit in an int type? like the MSB (31st bit)?

  13. #13

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    44
    I understand the concepts of bitwise operators but I don't know the syntax.
    I already zeroed out my variable by initializing it to zero as a whole

    int x = 0; for example, and I want to know how to set the 31st bit to a particular value. Can someone please help me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abstract data structure question
    By Shadow20 in forum C Programming
    Replies: 17
    Last Post: 03-26-2011, 09:12 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. question about data structure
    By renaissance in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2005, 01:47 PM
  4. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  5. Data structure question.
    By ronenk in forum C Programming
    Replies: 9
    Last Post: 10-03-2004, 10:58 AM