Thread: malloc() - arrays

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    malloc() - arrays

    Hi,
    I've been working on a program that asks the user to define the size of the array they would like to make and then input the elements, but skips the input of the elements. What am I overlooking here. My loop seems just fine. It's my first time using the function memory allocation, malloc(); something wrong there?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    	long int size;
    	long int buffer;
    	long int *pNumber = (long int *) malloc(size*sizeof(long int));
    
    int getSize()
    {
    	printf("How many elements are in the array? ");
    	scanf("%l", &size);
    	return (size);
    }
    	
    int getElements()
    {
    	printf("Please enter the elements of the array: \n");
    	for(int i = 0; i < size; i++)
    	{
    		scanf("%l", *(pNumber));
    	}
    	return(*pNumber);
    }
    
    int main()
    {
    	getSize();
    	getElements();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do not use global variables. When you change to use local variables, you probably would also spot your mistake.
    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
    Oct 2010
    Posts
    42
    I have since for testing purposes just brought everything into my main method, i tries debugging and notices that the pNumber value, does not change whilst in the loop, regardless of the user inputting the values. which has led me to believe they are not being stored on in memory at all.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    	int size;
    	int *pNumber;
    
    	printf("How many elements are in the array? ");
    	scanf_s("%i", &size);
    
    	pNumber = (int *) malloc(size*sizeof(int));
    
    	for(int i = 0; i < size; i++)
    	{
    		printf("Enter an element of the array: ");
    		scanf_s("%i", (pNumber+i));
    	}
    
    	printf("%i \n", pNumber);
    
    }
    Is this line wrong?

    scanf_s("%i", (pNumber+i));

    apologies for being a pest, the example in the c book i bought uses values which are hard coded into the program.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    I got it just need to figure out how to print the values in reverse order now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2-D Arrays and Malloc
    By CProgramming11 in forum C Programming
    Replies: 7
    Last Post: 11-06-2010, 09:14 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  4. malloc ing array's or structures
    By Markallen85 in forum C Programming
    Replies: 4
    Last Post: 02-03-2003, 01:23 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM