【问题标题】:How can I access a (const char*) from a function within other function?如何从其他函数中的函数访问 (const char*)?
【发布时间】:2019-12-18 08:41:45
【问题描述】:

我正在尝试访问在function1 中创建的const char* function2,以便将其用于某些操作。出于多种原因,它必须处于不同的功能中。

我尝试在function2 中使用此代码:

const char* str[1024] = { function1() };

但我没有任何成功。如果我尝试

printf("%s\n", str[1]);

function2 中,它只打印(null)

我也尝试过使用malloc,但没有成功。

//main function {it does its thing, it wouldn't interest us}
//function1 {it creates a const char* var[1024];)
//function2 {here I want to use the const char* var[1024]; from function1}

观察:在function1 中,const char* 可以很好地打印它需要打印的内容。

我希望我能找到解决这个问题的方法。感谢您的耐心等待!

稍后编辑代码:

const char* function1()
{
    lang = fopen("lang.csv", "r");
    int i = 0;
    char line[1024];
    const char* word[1024];
    char num[] = { 1 , 2 };
    while (fgets(line, 1024, lang))
    {
        char* tmp = _strdup(line);
        printf("Field 1 would be %s\n", getfield(tmp, num[0])); // NOTE strtok clobbers tmp
        word[i] = getfield(tmp, num[0]);
        i++; 
        free(tmp);
    }
    printf("%s\n", word[1]); //prints successfully
    fclose(lang);
    return NULL;
}

int function2() {
    const char* word[1024] = { function1() };
    printf("%s\n", word[1]); // failure, prints (null)
}

【问题讨论】:

  • 描述您尝试过的代码并描述结果。如何发布您尝试过的实际代码。它可能会让您更清楚您要做什么,因为const char* 是一种数据类型 - 您不能“call”一种数据类型,因此完全不清楚您在问什么。大概您只是想访问数组而不是调用它?如果是这样,您希望进行哪种访问 - 读取或写入或两者兼而有之?
  • 这是一个 X-Y 问题。您正在描述您在您的解决方案中遇到的问题,该解决方案是您未告诉我们的其他问题。描述你真正想要达到的目标,而不是你试图如何实现它。
  • 为什么function1 返回NULL
  • @melpomene 道歉,我想说“访问”。我只是很累。
  • @melpomene :很明显他是个新手,但这不是犯罪。回答这个问题是一个教育他的机会,不要因为他不知道而忽视他,不知道他不知道。然而,他可能会澄清这个问题,以便重新开放。就这么简单,他的代码的意图很清楚(足够),即使它在语义上是无意义的。

标签: c arrays string function char


【解决方案1】:

你有很多问题。

  • word 不是 static 所以在 function1() 之外不存在
  • 即使word 是静态的,它的元素也会指向tmp 中的内容,即free()function1() 返回之前的内容。
  • function1() 返回 NULL - 大概是为了返回指针 word?但由于上述所有原因,这无论如何都是不正确的。
  • function2() 分配的 word 指针与文件中的行数一样多,可能超过分配的 1024。

这个问题可以通过多种方式解决。一种方法是将指向单词的指针数组传递给function1() 并让function1() 为每个单词分配空间。这需要function2() 负责在完成后释放分配的字。这不是我通常推荐的方法,但另一种方法是为每个单词定义一些任意固定的最大长度。

我对@9​​87654335@ 的语义做了一些假设,并删除了数组num,因为它在原始代码中似乎没有任何作用。

int function1( const char** words, size_t max_words )
{
    FILE* lang = fopen( "lang.csv", "r" );
    char line[1024];
    size_t i = 0;
    while( i < max_words && fgets( line, 1024, lang ) != NULL )
    {
        printf( "Field 1 would be %s\n", getfield( line, 1 ) ) ;
        const char* tmp = getfield( line, 1 );
        char* word = malloc( strlen( tmp) + 1 ) ;
        strcpy( word, tmp ) ;
        i++ ;
    }
    printf( "%s\n", words[1] ); //prints successfully
    fclose( lang );

    return i ;
}

int function2() 
{
    char* word[1024] = {0} ;

    // Fill the word list
    int count = function1( word, sizeof(word) / sizeof(*word) ) ;

    // Print first word
    printf( "%s\n", word[1] ); //failure, prints (null)

    // Delete list elements allocated by function2()
    for( int i = 0; i < count; i++ )
    {
        free( word[i] ) ;
    }

    return count ;
}

【讨论】:

  • char *word[1024]const char** words 的转换不是隐式的(很遗憾):/ 您只能将其隐式转换为char * const * words
【解决方案2】:

这是一个独立的示例,向您展示如何从堆中分配单词列表并将其返回给另一个函数:

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

#define WORD_COUNT 1024

const char **function1(void) {
    const char **word = malloc(sizeof(const char *) * WORD_COUNT);
    for (int index = 0; index < WORD_COUNT; index++) {
        // You would replace these lines with your getfield code
        char number[20];
        snprintf(number, sizeof(number), "%d", index);
        word[index] = strdup(number);
    }
    // We return word here because that is how we pass the result to the caller
    return word;
}

void function2(void) {
    const char **word = function1();
    printf("word[1] = %s\n", word[1]);
}

int main(void) {
    function2();
    return 0;
}

输出

word[1] = 1

警告

程序通过mallocstrdup 分配内存,但没有调用free 来解除分配。这可能会导致内存泄漏。

【讨论】:

  • 这会泄露很多分配。
  • 如果你想解释返回值,调用这两个变量word并不是最好的选择。
猜你喜欢
  • 2015-07-11
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 2018-03-07
  • 2018-10-16
  • 2021-02-05
  • 2013-09-13
相关资源
最近更新 更多