cinny
cinny

Reputation: 2412

Global variables and Return multiple variable length arrays (pointers) in C function

I have some programming experience but not much with C.

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.

This program does a lot of computations on several long arrays of double with variable length, so they are all pointers. Two questions:

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?

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:

struct pointers {
    double *p1, *p2, *p3;
} ptr;

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

struct pointers *foo(double *input)

or

void foo1(double *input, struct pointers ptr)

or

void foo1(double *input, struct pointers *ptr)

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?

Should I initialize ptr->p1, ptr->p2, ptr->p3 inside or outside of foo1?

Note that foo() is the main function that will call foo1, foo2 sequentially.

Upvotes: 2

Views: 1037

Answers (4)

Jay D
Jay D

Reputation: 3307

To answer question

1 ) it really depends upon the size and scope of your data. If you have small amount of to be used as part of computation within your function it makes more sense to initialize it within the function thus limiting its scope to the function .

If the data size is few megabytes or even more than tens of kilobytes it makes sense to allocate on a heap using a malloc.

You can have Global variables in the file but then you have to worry about synchronizing the access to them -- who is modifying them and in which order --

To answer question 2) the better way to write a function signature would be

struct  {
    double output1; 
    double output2; 
    double output3;
 } output_t;

and the Function signature as:

ReturnCode foo ( double input , struct output_t * output);  

Where ReturnCode could be just and int value indicating whether the operation was successful or failed.

Upvotes: 1

Cacho Santa
Cacho Santa

Reputation: 6914

1- Global variables are highly discouraged in C also. These are two important reasons:

  • Most of the time you do not need to have variables available throughout the complete program, so you really don't need to make the variables global.
  • With local variables your code will be more maintainable and also you will be avoiding possible side effects.

But in your case, as you said ptr.p1, ptr.p2, ptr.p3 will become inputs to many other subsequent functions, may be it is a better decision to make it global. In that case, if you are defining global(only in that file), you should try the static modifier. Here you can read about static(it can have 2 different meanings)

2.I think that your second question "Having input and output both as parameters - is it confusing?" it actually depends on the developer, the language gives you both options but it depends on you to make clear code.

Upvotes: 1

jFuad
jFuad

Reputation: 11

specially in C, global variables are highly discouraged, unless you are working on multiple files each having to store their data. it is much easier to group data in structs and pass them to functions and its makes the whole thing clear to understand

here is a sample of a code i wrote, it deals with working with structures. only the declarations of the functions are here but you get the sense

struct elem {
    void *data;
    struct elem *next;
};

struct binky {
    struct elem *root;
    int elem_size;
};

void Initialize(struct binky * b, int elem_size);
int add(struct binky * b, const void * data);
void *get(const struct binky * b, const void * data);
void freeMap(struct binky * b);

Upvotes: 0

Abhinav Arora
Abhinav Arora

Reputation: 3391

Global variables are not advised in C too. What i can gather from your question is that foo1 will make the struct and foo2 will take it as input.

So in my opinion you should make foo1 return the struct and then pass the entire struct as a parameter to foo2

Upvotes: 0

Related Questions