简体   繁体   English

C:定义一个long作为返回类型函数的数组

[英]C: Define an array of long as return type of function

in C# I would just do: 在C#我会这样做:

private long[] myMethodName()
{
    //etc
}

What is the equivalent in C? C中的等价物是什么?

This gives an error: 这给出了一个错误:

long[] readSeqFile(char *fileName)
long *readSeqFile(char *filename);

You might want to make filename const: 您可能想要使filename名为const:

long *readFileName(const char *const filename);

UPDATE: while this answers the original question, it's not very good C practise. 更新:虽然这回答了原始问题,但这并不是很好的C练习。 If, as the poster says, he wants to read a list of integers from a file and get it back as an array of long, then the prototype is probably going to look something like this: 如果正如海报所说,他想要从文件中读取整数列表并将其作为长数组返回,那么原型可能看起来像这样:

int readSeqFile(const char *const filename,long **longArray,size_t *const len);

and the implementation (in sketch form): 和实施(草图形式):

#include <stdio.h>
#include <stdlib.h>

const size_t longArrayChunkSize=100;

int readSeqFile(const char *const filename,long **longArray, size_t *const len)
{
   FILE *fp;
   size_t nInts=0,curBufferLen;
   long tmp;

   fp=fopen(filename,"r");
   if(fp==NULL)
      return -1;

   *longArray=malloc(longArrayChunkSize*sizeof(long));
   if(*longArray==NULL)
      return -1;

   curBufferLen=longArrayChunkSize;

   // let's assume file is a list of integers, one per line
   while(read from file and !feof())
       {
       (scan line into tmp)
       longArray[nInts++]=tmp;
       if(nInts==curBufferLen)
          (realloc *longArray by longArrayChunkSize, increase curBufferLen)
       }

   *len=nInts;

   fclose(fp);

   return 0;
}

Call it like this: result=readSeqFile(filename,&longArray,&arrayLen); 这样调用: result=readSeqFile(filename,&longArray,&arrayLen); longArray is of type long* and it is the caller's responsibility to free it afterwards. longArray的类型为long* ,然后调用者有责任将其释放。 The result is zero on success and -1 on error. 成功时结果为零,错误时结果为-1。

This is where you win with C++ and STL classes like vector<long> since you can just push_back() each long as it comes out of the file without having to worry about memeory management. 这是你使用C ++和STL类(例如vector<long>获胜的地方,因为只要push_back()从文件出来就可以,而不必担心内存管理。

Typically for C you would pass a pointer and length 通常对于C,您将传递指针和长度

// returns number of elements written
size_t myMethodName(long* dest, size_t len)
{
    //etc
}

Nevermind.. I got it: 没关系..我明白了:

long *readSeqFile(char *fileName)

Thanks anyway 不管怎么说,还是要谢谢你

In C, functions cannot return array types; 在C中,函数不能返回数组类型; they can return pointers to arrays, such as 他们可以返回指向数组的指针 ,例如

long (*readSeqFile(char *filename))[N]
{
  ...
}

the downside being that N must be a compile time constant if you're using a C89 or earlier compiler, or a C2011 compiler that doesn't support variable length arrays. 缺点是,如果您使用的是C89或更早版本的编译器,或者不支持可变长度数组的C2011编译器,则N必须是编译时常量。 You probably don't want to go this route if you don't know how big the array is supposed to be ahead of time. 如果你不知道阵列应该提前多大,你可能不想走这条路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM