Question:
Arrays in C programming?
pytho
2011-09-23 22:35:58 UTC
Hi, I need to create a program but can't figure it out.. It will prompt the user for 3 things: 1) "Enter the size of the arrays: " -- this will be an integer, s. 2) "Enter the cells of the first array: " -- user will enter s integers [seperated by whitespaces] that will signify array1[0], array1[1], etc. 3) "Enter the cells of the second array: " -- user will enter integers of the same format as in #2.

This is not the purpose of the entire program, but I can do the rest. I just don't know how to store the input values from #2 and #3 into integer arrays.. because there are whitespaces involved. I can only use scanf() and the arrays must be integers [not strings]. Please help! I appreciate it-
Three answers:
MichaelInScarborough
2011-09-24 02:32:33 UTC
Here is some code, which could be helpful.



What it does:

It queries the array size and then uses calloc in order to allocate memory for the array.

In the loop scanf is picking up a maximum of iArraySize integers.



This line cleans out the input buffer passed to scanf: while (getchar() != '\n'),



For scanf the delimiter is important in order to distinguish between the different numbers, else it reads until eof (or here '\n') is encountered.



The free at the bottom of the code is important in order to avoid memory leaks. Dynamically allocated memory resources must be freed explicitly.



Memory allocation/deallocation for the second array you referred to follows the provided code sample.



I hope that helps.





#include

#include

#include



int get_number(const char *szPrompt)

{

int iRet = 0;

int iScanRet = 0;

do {

printf("\n%s: ", szPrompt);

iScanRet = scanf("%d", &iRet);

while(getchar() != '\n');

} while(iScanRet != 1);

return iRet;

}



int main()

{

int iArraySize = get_number("How many integers do you want to enter? > ");

int i = 0;

int *piArray = (int *)calloc(iArraySize, sizeof(int));



for (i = 0; i < iArraySize; i++) {

scanf("%d", &piArray[i]);

}

while (getchar() != '\n');

for (i = 0; i < iArraySize; i++) {

printf("[%d] ", piArray[i]);

}

free(piArray);



return 0;

}
divaraj
2016-12-02 05:02:32 UTC
once you declare an array int array[5] then you definitely are telling the compiler to set aside 5 contiguous reminiscence places which could each carry an int. you may placed characters into it yet printing it and utilising customary string applications won't artwork. characters are in simple terms small integers -- usually 0 to 255 selection. you may't placed a waft into it because of the fact the sizeof a waft isn't the comparable as an int. And the compiler has to comprehend what to do with the reminiscence region. if your array is of form char then each reminiscence region is one byte in length -- you may placed integers into it as long as they're interior the selection of 0 to 255 (for unsigned char) or -127 to 127 for signed char. char data form is an integer form that usually includes the ASCII character set codes. An array is a contigiuos piece of reminiscence which you will keep issues in. all of the products interior the array must be the comparable length so your programme can discover them An array index is used as an offset into the reminiscence waft ar[10]; ar can carry ten floating component values -- the 1st being at reminiscence region ar -- the final at reminiscence region ar+ 9*sizeof(waft) bytes from the place the array starts -- referred to as ar consequently. in case you particularly need to keep categories of data in an array you need to use an array of void * void * array[20]; it particularly is an array of 20 addresses -- each handle can component to a distinctive form of data. when you consider that addresses have the comparable length this could artwork -- the information they component to could have distinctive sizes. to apply the information you need to forged the value pointed to to the form you pick. char c; .... c= (char) *array[10]; HTH
?
2011-09-23 22:53:52 UTC
You need to research dynamically allocated arrays

and use malloc or calloc and perhaps realloc

they are kind of clunky to use.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...