我是學習 C 的新手。我在從函式回傳雙星指標時遇到問題。
這是我的代碼。
#include <stdio.h>
#include <stdbool.h>
#include "api.h"
void init() {
// You can initiate and calculate things here
}
/**
* Return the given list in reversed order. Each element in the given list contains a single character.
*
* typedef struct {
* int count;
* const char * * elements;
* } string_list;
*
* list_of_chars: string_list
*
* returns: string_list
*/
string_list reverse(string_list list_of_chars) {
// Write your code here
char X[list_of_chars.count];
//X[0] = *list_of_chars.elements[0];
for (int i = 0; i < list_of_chars.count;i ){
X[i] = *list_of_chars.elements[i];
}
printf("%c", X[0]);
string_list return_value = {
.count = 0,
.elements = &X[0],
};
return return_value;
}
但是我正在從不兼容的指標型別'char *'中初始化'const char **'。我嘗試了多種解決方案,但沒有一個有效。
uj5u.com熱心網友回復:
旁注:正如我在熱門評論中提到的,我只能在移動設備上做些什么。我回到我的辦公桌前。
問題是:
X是函式作用域,所以當函式回傳時它會超出作用域。我們需要使用malloc[所以我們需要stdlib.h]- 因為
elements是 aconst char **,所以我們需要const char **X
我們需要使用 [should use]strdup來復制元素。
否則,兩個串列將共享相同的資料。這可能是期望的意圖。但是,從問題描述中不清楚正確的代碼應該是什么。
大多數串列應該彼此完全獨立。原始代碼不這樣做。因此,串列正在共享資料。但是,如果這是真的,為什么還要創建一個單獨的串列呢?我們可以通過反向索引來“反轉”它而不創建新串列:
for (int i = list.count - 1; i >= 0; --i)
所以,我選擇復制字串。使用 .編譯時的原始行為(共享字串)-DNODUP。
這是重構的代碼:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#if 0
#include "api.h"
#else
typedef struct {
int count;
const char **elements;
} string_list;
#endif
void
init()
{
// You can initiate and calculate things here
}
/**
* Return the given list in reversed order. Each element in the given list contains a single character.
*
* typedef struct {
* int count;
* const char * * elements;
* } string_list;
*
* list_of_chars: string_list
*
* returns: string_list
*/
string_list
reverse(string_list list)
{
const char **X = malloc(sizeof(*X) * list.count);
// share the strings
#if NODUP
for (int i = 0; i < list.count; i )
X[list.count - 1 - i] = list.elements[i];
// make lists independent (more usual/expected)
#else
for (int i = 0; i < list.count; i )
X[list.count - 1 - i] = strdup(list.elements[i]);
#endif
string_list ret = {
.count = list.count,
.elements = X,
};
return ret;
}
更新:
在上面的鏈接中是練習頁面,我們可以在其中為 C 選擇 GCC。我將您的代碼完全粘貼到網站位仍然看到相同的錯誤。
好的,最后...... ;-)
- 該網站使用 C 標準的古老版本(例如 C99)。這個沒有
strdup。_ - 該網站會在包含代碼之前[強制]做一個
#include "api.h",因此與我使用的定義有沖突。
這是更正后的代碼(在網站上成功運行):
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#if 1
#include "api.h"
#else
typedef struct {
int count;
const char **elements;
} string_list;
#endif
void
init()
{
// You can initiate and calculate things here
}
/**
* Return the given list in reversed order. Each element in the given list contains a single character.
*
* typedef struct {
* int count;
* const char * * elements;
* } string_list;
*
* list_of_chars: string_list
*
* returns: string_list
*/
string_list
reverse(string_list list)
{
const char **X = malloc(sizeof(*X) * list.count);
// share the strings
#if 1
for (int i = 0; i < list.count; i )
X[list.count - 1 - i] = list.elements[i];
// make lists independent (more usual/expected)
#else
for (int i = 0; i < list.count; i )
X[list.count - 1 - i] = strdup(list.elements[i]);
#endif
string_list ret = {
.count = list.count,
.elements = X,
};
return ret;
}
旁注:我會買一本關于 C 的好書并學習它以獲得更多的 C 基礎知識。嘗試使用“競爭”網站并不是最好的方法,IMO。
如果您對 C 語法和語意有更好的基本理解,那么您已經看到(但不理解)的許多問題會很明顯。
這是一個串列:權威 C 書籍指南和串列
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474616.html
上一篇:將函式應用于多維陣列的每個元素
下一篇:在Julia坐標串列中轉換影像
