Thread: declaring a variable and allocating

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    12

    declaring a variable and allocating

    hello, im trying to declare a pointer pointing to an array of structures ĘstructĘ, that array has 200 elements so i need to allocate memory for those 200 elements but i dont seem to get it right:
    Code:
    typedef struct str{
          int nbits;
          }codification;
    
    codification **hello;

    where do i use malloc?
    for example this how i would use it, i think:
    Code:
    (*hello)[0].nbits=4;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    If you know for sure that you're dealing with an array of 200 objects, then choose the simplest approach:
    Code:
    #define NUM_CODIFICATIONS 200
    
    codification codifications[NUM_CODIFICATIONS];
    If you don't know at compile time what the number will be, so 200 is just a concrete example or initial number, then:
    Code:
    size_t num_codifications = 200;
    codification *codifications = malloc(sizeof(*codifications) * num_codifications);
    But remember to check that codifications is not a null pointer before using it.

    Either way, you would use it like this:
    Code:
    codifications[0].nbits = 4;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    12
    thank you, but it needs to be a double pointer, thats what they are telling me to use, thats what confuse me

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Since you want "a pointer pointing to an array of structures", I'm afraid that they are wrong, whoever they may be. In the most literal interpretation of what you asked for, you would write:
    Code:
    codification (*codifications)[200];
    i.e., a pointer to an entire array of 200 struct objects, but this is unlikely to be what you need unless you're traversing over an array of arrays of 200 objects. So, my suggestions are more likely the way to go.

    On the other hand, if you want an array of pointers, then a pointer to a pointer would be an option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > but it needs to be a double pointer
    Why?

    > hello, im trying to declare a pointer pointing to an array of structures ĘstructĘ, that array has 200 elements
    A pointer to an array does not make it a double pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2020
    Posts
    12
    honestly i need to use it for a proyect in class, i know its declared like this "codification **hello" because they give us the head of the functions we have to make in some of them it appears like this "codification **hello" and in other functions like this "codification *hello", in both cases i need to be able to modify the array, i dont really get the concept behind it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Quote Originally Posted by stanley1234
    i know its declared like this "codification **hello" because they give us the head of the functions we have to make in some of them it appears like this "codification **hello" and in other functions like this "codification *hello", in both cases i need to be able to modify the array
    Ah, now that's different. If hello is a parameter, then you do need it to be a pointer to a pointer even when allocating for a dynamic array of codification objects because you want the pointer from the caller to be modified. But that doesn't mean that you have to use it all the way. For example:
    Code:
    void init_codifications(codification **hello, size_t num_codifications)
    {
        codification *codifications = malloc(sizeof(*codifications) * num_codifications);
        // do other stuff, e.g., set initial values for codifications[i] in a loop
        *hello = codifications;
    }
    If you only have it as a pointer to codification parameter, then you can modify the content of the dynamic array, but you cannot modify the pointer to the dynamic array from the caller.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2020
    Posts
    12
    thank you, that was it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring Variable
    By erdemtuna in forum C Programming
    Replies: 2
    Last Post: 12-04-2015, 02:48 AM
  2. Declaring variable in a for loop
    By GokhanK in forum C Programming
    Replies: 4
    Last Post: 08-01-2013, 04:38 PM
  3. SQL Exception ( declaring a variable )
    By Aga^^ in forum C# Programming
    Replies: 3
    Last Post: 07-21-2009, 03:49 AM
  4. Does declaring a variable cause lag in the executable?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2005, 08:15 PM
  5. Conditional Variable Declaring
    By ibelievekip in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 11:38 AM

Tags for this Thread