English 中文(简体)
C[复制]功能中的回归阵列
原标题:Return array from function in C [duplicate]
  • 时间:2012-01-14 22:45:16
  •  标签:
  • c
  • pointers
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Declaring a C function to return an array

我是新到的,需要你们的想法,帮助我恢复以下职能所产生的成果:

void getBase(int n, int b)
{
    const size_t SIZE = 32;
    char arr[32+1]={0}; int digits=SIZE, i;
    char* ptr = arr;
    while (n > 0)
    {
        int t = n%b;
        n/=b;
        arr[--digits] = numbers[t];
    }
    while ( *ptr ==   ) ptr++;
    // NEED To return a ref to `ptr`
}

我的解决办法:

void getBase(int n, int b, /*send some  array as a parameter*/ char* str)
{
    const size_t SIZE = 32;
    char arr[32+1]={0}; int digits=SIZE, i;
    char* ptr = arr;
    while (n > 0)
    {
        int t = n%b;
        n/=b;
        arr[--digits] = numbers[t];
    }
    while ( *ptr ==   ) ptr++;

    /* and use strcpy ... perhaps memcpy if non-string )*/
    strcpy(str, ptr);
}

我需要进一步的想法......。

感谢。

最佳回答

你们的解决办法是严厉的。

Instead, you don t even need the local arr array at all. You can just write directly into str:

www.un.org/Depts/DGACM/index_spanish.htm EDIT :清洁和工作版本。

const char numbers[] = "0123456789abcdef";

void getBase(int n, int b, char* str)
{
    const size_t SIZE = 32;
    int digits=SIZE;
    while (n > 0)
    {
        int t = n%b;
        n/=b;
        str[--digits] = numbers[t];
    }

    int length = SIZE - digits;

    memmove(str,str + digits,length);
    str[length] =   ;
}

You just have to make sure that your str is large enough to avoid an array-overrun.


int main(){

    char str[33];

    getBase(684719851,10,str);

    printf(str);

    return 0;
}

<<>Output>:

684719851
问题回答
  1. 换言之,共同的解决办法是分配一个阵列,让它有一个点。 确保你在打电话时释放。

  2. 如果你(在汇编时间)知道阵列的规模,你就可以制造一个含有阵容的构体,并返回结构。 它指出,它将把阵列推向 st,并可能放慢方案。 如果它是一个真正庞大的阵列,那么你甚至可能出现 st流。

如果你想恢复一阵的长度,你就应该首先与小区一道建立阵列,然后你可以回来。 E.g.

char *arr = malloc(n*sizeof(char));

//now arr points to an array, use it as you like

return arr;

• 确保把你的职责的返回类型确定为果园。 对于任何种类的阵列,你们都可以做同样的事情——确保相应地更新所有类型。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签