Pointers and Data Structures (Simple Question, I think)

This is a discussion on Pointers and Data Structures (Simple Question, I think) within the C Programming forums, part of the General Programming Boards category; I have a pointer going to a data structure. The data structure has a member that is a double array. ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Pointers and Data Structures (Simple Question, I think)

    I have a pointer going to a data structure. The data structure has a member that is a double array.

    For the region my pointer points to, if I want to assign an element of the double array a value, how do I go about doing it?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Are you talking about something like this?

    Code:
    struct SomeRandomStruct
    {
        int data[10][10];
    };
    
    //... later in the main program...
    
    SomeRandomStruct* mystruct = new SomeRandomStruct;
    mystruct->data[0][0] = 5;

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Yes, like that. When I said double array, I actually meant an array of a double variable. Hehe...Sorry for the confusion.

    I've been getting by using more of this though:
    Code:
    struct randomStruct
    {
        double data[9000];
    };
    
    //... later in the main program...
    
    struct randomStruct *pointer = new randomStruct;
    (*pointer).data[0] = 4.0;
    Although, they do the same thing, right?


    Another question, is it possible to use dynamic storage allocation for the members of your data structure? And if so (which I'm rather positive it is), how would one go about implementing it?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    There is no 'new' keyword in C.

    But I think you could do something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int size = 100;
    
            struct randomStruct{
                    double *data;
            };
    
            struct randomStruct randomStruct1;
            randomStruct1.data = malloc(size *sizeof(double));
    
            if(randomStruct1.data){
                    printf("size of struct: %d\n", sizeof(*randomStruct1.data));
            }
    
            randomStruct1.data[0] = 1.234;
            randomStruct1.data[1] = 5.678;
    
            printf("Content of randomStruct1.data[0] and [1]: %f, %f\n", randomStruct1.data[0], randomStruct1.data[1]);
    
            return 0;
    }
    How you would figure out the size, will differ though.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you don't need dynamic allocation you could do like this instead:

    Code:
            struct randomStruct{
                    double data[10];
            };
    
            struct randomStruct randomStruct1;
    
            randomStruct1.data[0] = 1.234;
            randomStruct1.data[1] = 5.678;
    
            printf("Content of randomStruct1.data[0] and [1]: %f, %f\n", randomStruct1.data[0], randomStruct1.data[1]);

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    BTW, if you use malloc, don't forget to free it when your done with it.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Quote Originally Posted by Subsonics View Post
    There is no 'new' keyword in C.
    Really? My program crashes if I don't have '= new struct randomStruct' in the declaration. How...peculiar. I thought there was no 'new' keyword either, but I noticed it in scwizzo's post, so I tried it and it fixed the first error I had.

    So, to make malloc work with the member, I would have to make the member a pointer then? And I can still declare a pointer that points to the structure, right? So...
    Code:
    struct randomStruct
    { double *data
    }; randomStruct
    
    //...Later on
    
    int size = 9000;
    struct randomStruct *randomStruct1;
    (*randomStruct1).data = malloc(size *sizeof(double))
    Would that still work?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by Flamefury
    Really? My program crashes if I don't have '= new struct randomStruct' in the declaration. How...peculiar. I thought there was no 'new' keyword either, but I noticed it in scwizzo's post, so I tried it and it fixed the first error I had.
    That implies that you are compiling as C++ instead of compiling as C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Flamefury View Post
    Code:
    struct randomStruct
    { double *data
    }; randomStruct
    
    //...Later on
    
    int size = 9000;
    struct randomStruct *randomStruct1;
    (*randomStruct1).data = malloc(size *sizeof(double))
    Would that still work?
    Now you have a pointer of the type randomStruct, you would still need it to point at an actual randomStruct.

    So for example:

    Code:
    struct randomStruct randomStruct1;
    struct randomStruct *randomStructPtr;
    
    randomStructPtr = &randomStruct1;
    (*randomStructPtr).data = malloc(size *sizeof(double));
    Should work, I think.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Quote Originally Posted by laserlight View Post
    That implies that you are compiling as C++ instead of compiling as C.
    That would explain why I just failed the compiler test for handing my assignment in. Hmm.
    This is the gist of what I have right now:
    Code:
    struct randomStruct *randomCreate() {
    struct randomStruct *myStruct;
    return myStruct;
    }
    int main (void)
    {
       struct randomStruct *p = randomCreate();
    }
    And it isn't co-operating without a 'new randomStruct'. How do I make it work under C terms?

    Subsonics, I'll give that a shot.

    Thank you for everyone's help so far! I really appreciate it.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What is it you are trying to do exactly? Right now you are assigning the address of an empty struct pointer to *p. IMO it's no different to just doing this:

    Code:
    struct randomStruct *p;

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    I'll start talking about my assignment because it's getting hard to keep to general terms.

    I want to create a program that manipulates polynomials. The polyCreate() function returns a pointer to (what I would assume to be) a zero-ed polynomial data structure, which will later have values added to it, multiplied, etc.

    Right now, I have in my data structure one member, the coefficients, and I use it as an array to keep track of what degree the x value is that it multiplies by. So, in my polyCreate() function, I create the pointer and allocate the memory, then I zero all the values inside the coefficients array of the pointer's variable.

    Although, if I try to call the polyCreate() function, my program crashes on startup. I just attempted to create a specific variable that the pointer points to, but it still doesn't work...

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you do like that you will get a pointer to the same datastructure each time you call polyCreate(). Structs are no different than any other data types in that respect. There are no constructors in C.

    Try doing the same with a char for example, like this.
    Code:
    #include <stdio.h>
    
    char *charCreate(){
            char b = 'a';
            char *a = &b;
            return a;
    }
    
    int main (void)
    {
            char *c = charCreate();
            char *d = charCreate();
    
            printf("Address of c and d: %p, %p\nValue of c and d: %c, %c\n", c, d, *c, *d);
    
            return 0;
    }
    Then you'll see that both calls to charCreate, returns the same address to the same variable.

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    I see. I had my suspicions it worked that way, and now I know for sure. Perhaps that's the reason my program is crashing?

    In that case, what can I do to change the address that the pointer goes to?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Flamefury View Post
    That would explain why I just failed the compiler test for handing my assignment in. Hmm.
    This is the gist of what I have right now:
    Code:
    struct randomStruct *randomCreate() {
    struct randomStruct *myStruct;
    return myStruct;
    }
    int main (void)
    {
       struct randomStruct *p = randomCreate();
    }
    And it isn't co-operating without a 'new randomStruct'. How do I make it work under C terms?

    Subsonics, I'll give that a shot.

    Thank you for everyone's help so far! I really appreciate it.
    Seems pointless to work on your program, when you're using the wrong compiler. Get the compiler right, *first*. Then you can start making progress on the program.

    Your program should have options to set which compiler (C or C++), it will use. Get those options set and be sure to keep your file extension for your program dot c and *NOT* dot cpp.

    "new" can be your test, also. If it immediately causes an error, then you should be using the C compiler. If not, you're still using the wrong (C++) compiler.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Replies: 2
    Last Post: 01-26-2003, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21