在C函数的全局变量和回报倍数变长数组(指针) [英] Global variables and Return multiple variable length arrays (pointers) in C function

查看:134
本文介绍了在C函数的全局变量和回报倍数变长数组(指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些编程经验,但没有太大的用C

I have some programming experience but not much with C.

我有一个相当大的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),它们从一开始就计算一次,然后以此为依据许多后续函数的变量 - 我应该让他们这个文件中的全局变量?从更高层次的语言我的编程经验,全局变量都不好。这难道不是对C,为什么这样呢?

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)当我的函数的1希望多个指针(每分返回到长度为n为例),说双* P1,双* p2的,即是相关双* P3,我可以将它们组合成一个双阵列一个结构:

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将采取双*输入作为输入,并计算ptr-> P1,ptr-> P2,ptr-> P3,然后PTR将在后面作为foo2的输入。我应该写

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)

void foo1(double *input, struct pointers ptr)

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

为什么说C函数通常是无效的功能,除非它返回只是一个int或double变量?其输入和输出都作为参数 - 它是混乱

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?

我应该初始化ptr-> P1,ptr-> P2,ptr-> P3内部或foo1之外吗?

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

需要注意的是富()是将调用foo1的主要功能,依次foo2的。

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

推荐答案

要回答的问题。

1)它真的取决于你的数据的尺寸范围。如果你有被用来作为你的函数内计算的一部分少量更有意义,因此会限制其范围的函数的函数中进行初始化。

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 .

如果数据大小为几兆或者几十比千字节是有意义的分配使用一个malloc堆甚至更多。

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 --

要回答的问题2)更好的方式来写一个函数签名会

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

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

和函数签名为:

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

当返回code可能只是和int值指示操作是否成功还是失败。

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

这篇关于在C函数的全局变量和回报倍数变长数组(指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆