Note that there are some explanatory texts on larger screens.

plurals
  1. POPass by referencing not maintaining its integer value
    text
    copied!<p>I am making a new question because i deemed it to be a different topic to my one about my earlier problem here: <a href="https://stackoverflow.com/questions/20195482/c-trouble-looping-through-arrays-define-in-other-source-files">Trouble looping through arrays define in other source files</a></p> <p>The problem i current have is that i pass in a size_t value by reference to another function, i then set the value of said size_t inside that function, which i can then you in another function.</p> <p>The problem i am facing is that when i pass in the size_t variable, the function that sets the value sets it's value correctly but when i return to source file where the variable is declared it has "random" values again.</p> <p>Anyone have any ideas why this is happening?</p> <p>system_handler.c</p> <pre><code>size_t ship_size; size_t asset_size; mayday_call* mday_ptr; ship* ship_ptr; rescue_asset* assets_ptr; mday_ptr = read_mayday_file(); ship_ptr = read_ship_locations(&amp;ship_size); assets_ptr = read_recuse_assets(&amp;asset_size); printf("ships size : %d\n", ship_size); printf("assets size : %d\n", asset_size); </code></pre> <p>ship.c</p> <pre><code>ship* read_ship_locations(size_t* size){ //no_of_lines is an unsigned int //locof is a char array containing a file name no_of_lines = (count_lines(locof) -1 ); printf("number of lines = %d \n", no_of_lines); size = (unsigned int)no_of_lines; size = no_of_lines; } </code></pre> <p>rescue_assets.c</p> <pre><code>rescue_asset* read_rescue_assets(size_t* size) { //no_of_lines is an unsigned int //locof is a char array containing a file name no_of_lines = count_lines(locof); printf("number of lines = %d \n", no_of_lines); assets = calloc(no_of_lines,sizeof (rescue_asset)); size = (unsigned int)no_of_lines; printf("size : %d\n", size); } </code></pre> <p>Console Output:</p> <pre><code>please enter the file name for the ship locations data: ships_1.txt number of lines = 4 size : 4 Please enter the file name for the rescue assets data: rescue_assets.txt number of lines = 37 size : 37 ships size : 134513984 assets size : 0 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload