sorting - How to iterate through memory block (created with malloc) in C? -


my objective read unknown amount of integers text file, store them block of memory, sort them, , output new file. have program can read integers file , output them, , i'm trying implement sorting part. i'm coming class arts background, , professor's instructions on how use unclear me.

instructions: use malloc() assign memory block integer pointer. each time new integer read input file , current memory block not enough hold it, use realloc() assign more space memory block hold more integers. may use *(ptr+x) (an equivalent way use ptr[x]) access (x+1)th integer in memory block pointed integer pointer ptr. may apply sorting algorithm (e.g., selection sort or bubble sort) you've learned in previous courses sort these integers.

what i'm not understanding how iterate through allocated memory block in order sort it. x professor mentions? if clarify how use this? don't want post code avoid cheating. thanks!

int x;   // uninitialized variable (its content unknown) int count_of_item = 10 ; // initialized variable int *ptr=malloc(count_of_item*sizeof(int)); // ptr allocated memory block  for(x=0;x<count_of_item;x++) // iterating through memory block     printf("%x element =%d\n",x,ptr[x]);// , printing content                                          // x index in range                                          // 0 count_of_item-1. 

iterating through allocated memory block, accessing content in order read or make change on (write). basics of 'c' if stuck here, in troubles.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -