Thread: Assigning an array with a variable number of elements

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    5

    Assigning an array with a variable number of elements

    Hi.
    I am new on this forum.
    I have a simple problem about memory allocation.
    In the function Nr_elements() i assign a value which represent the elements of array. The pointer p is initialised with the address of variable n, but when i compile i dont know why but dont work.
    This function return a pointer.
    Can somebody help me ?
    Thank you.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int *Nr_elements();
    int *allocate(int);
    void deallocate(int *);
    
    
    int main()
    {
        int i, *tab, *tab1;
        tab = Nr_elements();
        tab1 = allocate(*tab);
        printf("\n The elements of array : ");
        for(i=0; i<*tab; i++)
            printf(" %d", *(tab1+i));
        printf("\n");
        deallocate(tab1);
        return 0;
    }
    
    
    int *Nr_elements()
    {
        int *p, n;
        p = &n;
        printf("\n Enter the number of elements : ");
        scanf("%d", &n);
        return p;
    }
    
    
    int *allocate(int n)
    {
        int i, *ptr;
        ptr = (int *)malloc(n * sizeof(int));
        for(i=0; i<n; i++)
        {
            printf("\n Ptr[%d] = ", i+1);
            scanf("%d", ptr+i);
        }
        return ptr;
    }
    
    
    void deallocate(int *ptr)
    {
        free(ptr);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In Nr_elements, p points to a local variable. This local variable ceases to exist after the function returns to the caller. It would be simpler and correct to write:
    Code:
    int Nr_elements(void)
    {
        int n;
        printf("\n Enter the number of elements : ");
        scanf("%d", &n);
        return n;
    }
    Later, you can change the code to check the return value of scanf.
    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
    Jul 2014
    Posts
    5
    Thank you for your interest.
    I have done the modification that you suggest me.

    Know i want to make a function that print me the elements of array.
    Look how i did it.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int Nr_elemente();
    int *aloca(int);
    void afiseaza(int, int *);
    void dealoca(int *);
    
    
    int main()
    {
        int i, tab, *tab1;
        tab = Nr_elemente();
        tab1 = aloca(tab);
        printf("\n The elements of array : ");
        afiseaza(tab, tab1);
        dealoca(tab1);
        return 0;
    }
    
    
    int Nr_elemente()
    {
        int n;
        printf("\n Enter the number of elements : ");
        scanf("%d", &n);
        return n;
    }
    
    
    int *aloca(int n)
    {
        int i, *ptr;
        ptr = (int *)malloc(n * sizeof(int));
        for(i=0; i<n; i++)
        {
            printf("\n Ptr[%d] = ", i+1);
            scanf("%d", ptr+i);
        }
        return ptr;
    }
    
    
    void afiseaza(int n, int *ptr)
    {
        int i;
        for(i=0; i<n; i++)
            printf(" %d", *(ptr+i));
        printf("\n");
    }
    
    
    void dealoca(int *ptr)
    {
        free(ptr);
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I hope the function names make sense in your native language, since they don't to me. (That's not a criticism, as such, simply an observation that your code needs to be comprehensible to you and people who might have to maintain it. For a native english speaker, your function names don't communicate much information about what they achieve).

    It's not generally necessary to write a raw wrapper for free(), since free() can accept any pointer that has been returned by malloc() and related functions - regardless of actual type of that pointer. If you're doing more than just a single free() call (or calling a function in a DLL) then a wrapper is useful. But that doesn't appear to be what you are doing.

    I suggest you need to give some thought and decide if pointer syntax ("ptr+i" or "*(ptr + i)") is more comprehensible than array syntax ("&ptr[i]" or "ptr[i]" respectively). There's no right or wrong answer to that (in fact, depending on what the code is doing, either form can be more comprehensible). However, if you're having to do a mapping (in your head) pointer syntax to array syntax or vice versa, then you're often better off using the syntax that you're mapping to.

    It is usually a good idea to check the pointer returned by malloc() (e.g. compare it with NULL) before storing data into it. Your aloca() function is calling malloc(), and then - in the loop - relying on malloc() not having returned NULL. It is a good idea to check if malloc() returned NULL before entering the loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning structure elements to an array
    By anndruu12 in forum C Programming
    Replies: 14
    Last Post: 12-08-2010, 05:25 PM
  2. Replies: 9
    Last Post: 05-29-2010, 10:32 AM
  3. Assigning Array Elements By Reference
    By bhenderson in forum C Programming
    Replies: 8
    Last Post: 08-23-2009, 03:21 PM
  4. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  5. assigning a rand number to each element in an array
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2002, 01:12 PM

Tags for this Thread