Note that there are some explanatory texts on larger screens.

plurals
  1. POGlobal variables and Return multiple variable length arrays (pointers) in C function
    primarykey
    data
    text
    <p>I have some programming experience but not much with C.</p> <p>I have a reasonably large C file. In it there are multiple functions that are executed sequentially - so in this particular case no function is called twice really, they are broken down for ease of reading, since each function still has a separate purpose.</p> <p>This program does a lot of computations on several long arrays of double with variable length, so they are all pointers. Two questions:</p> <p>1) The variables that are calculated once from the beginning and then serve as input to many subsequent functions - should I make them global variables inside this file? From my experience programming in higher level languages, global variables are not good. Is this not the case for C and why?</p> <p>2) When 1 of my function wants to return multiple pointers (each points to an double array of length n for example), say double *p1, double *p2, double *p3 that are related, I can combine them into a struct:</p> <pre><code>struct pointers { double *p1, *p2, *p3; } ptr; </code></pre> <p>foo1 will take double *input as input, and calculate ptr->p1, ptr->p2, ptr->p3, and then ptr will later serve as input for foo2. Should I write </p> <pre><code>struct pointers *foo(double *input) </code></pre> <p>or</p> <pre><code>void foo1(double *input, struct pointers ptr) </code></pre> <p>or</p> <pre><code>void foo1(double *input, struct pointers *ptr) </code></pre> <p>Why is it that C functions are usually 'void' functions, unless it returns just an int or double variables? Having input and output both as parameters - is it confusing?</p> <p>Should I initialize ptr->p1, ptr->p2, ptr->p3 inside or outside of foo1? </p> <p>Note that foo() is the main function that will call foo1, foo2 sequentially.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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