Initializing Dynamic (Allocated) Array?

This is a discussion on Initializing Dynamic (Allocated) Array? within the C Programming forums, part of the General Programming Boards category; Hello. Although I've already spotted this thread " http://cboard.cprogramming.com/c-pro...allocated.html " I could not understand why following code gets not compiled ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Question Initializing Dynamic (Allocated) Array?

    Hello.

    Although I've already spotted this thread "Initializing a 2D array dynamically allocated" I could not understand why following code gets not compiled with GCC (C99-standard):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void) {
    	int *array = malloc(2*sizeof(int));
    	array = {2,1};//error: expected expression before { token
    	free(array);
    	
    	return EXIT_SUCCESS;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by Mekeor View Post
    Hello.

    Although I've already spotted this thread "Initializing a 2D array dynamically allocated" I could not understand why following code gets not compiled with GCC (C99-standard):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void) {
        int *array = malloc(2*sizeof(int));
        array = {2,1};//error: expected expression before ‘{’ token
        free(array);
        
        return EXIT_SUCCESS;
    }
    Way off. I strongly suggest that you go read a C language reference first before trying this. Basics, basics, basics...
    Last edited by Sebastiani; 02-17-2011 at 02:35 PM. Reason: (emphasis mine)



    ITSA
    Socket Library!

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    If you spotted the thread, actually read it and noted that people said that it was not possible with a dynamically-allocated array, why did you

    a. insist on trying anyway
    b. complain when what you were told would happen actually happened?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Question Alternative?

    All right. Thank you very much for your replies. I've just one question left over:

    Do you know an alternative way to achieve my aim?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Nope, and it really doesn't make sense to. If you know how big you want your array to be at compile time (obviously you do, since you initialize exactly that number of elements), there's no point in dynamic memory, you would just use a regular array. Dynamic memory is for allocating stuff at run time, implying you don't know how many things you have until you're running your program, thus you can't have a valid initializer at compile time.

    You're trying to use the wrong tool for the wrong job.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Should be possible to use a compound literal with memcpy, but it will end up being more to write and be less clear than to simply do it manually.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by Subsonics View Post
    Should be possible to use a compound literal with memcpy, but it will end up being more to write and be less clear than to simply do it manually.
    Do explain.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What I meant was, (half jokingly) that this is a more complicated and in reality not practical solution:

    Code:
    memcpy(array, ((int []){2,1}), sizeof(int)*2);
    compared to this:

    Code:
    array[0] = 2;
    array[1] = 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. dynamic array of struct within dynamic array of struct
    By explodecomputer in forum C Programming
    Replies: 3
    Last Post: 08-03-2010, 03:25 AM
  3. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 04:25 PM
  4. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  5. Replies: 4
    Last Post: 11-02-2006, 10:41 AM

Tags for this Thread


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