gpt4 book ai didi

c - 将字符串指针传递给 C 中的函数时得到虚假结果

转载 作者:太空宇宙 更新时间:2023-11-04 08:42:46 65 4
gpt4 key购买 nike

我目前正在尝试学习 C,在尝试使用字符串时遇到了一些问题。问题如下:

  1. 我从用户的 get_question_answer 函数中检索一个字符串指针,并将其设置为指针 char *question,

  2. 然后在主要部分打印得很好。

  3. 但是当我将它传递给任何函数时,我会得到奇怪的结果。

  4. 在 new_func_to_test_scope 函数中,它打印大部分输入(16 个字符),然后添加一个 ¿。

  5. 最后,我的原始问题是,当将 *question 和 *answer 字符指针传递给函数 database_write 时,它​​打印的都是伪造的。

我怀疑这是一个范围问题,但我现在已经在这上面花了很长时间,但一直没能弄清楚是怎么回事。因此,我们将不胜感激。

#include "quizStruct.h"
#include <stdio.h>
#include <stdbool.h>

int correct_answer(const char *answer, struct quizData data);
char *get_question_input();
char *get_question_answer();
void database_print(struct database *db);
void database_write(struct database *db,int id, char *question, char *answer);
void strip_newline(char *string, int size);
void new_func_to_test_scope(char **string1, char **string2);
int i = 0;

int main(int argc, const char * argv[])
{
if(argc <3) printf("Please enter more inputs");

char *question;
char *answer;
struct database *db = database_create();

bool keepProgramRunning = true;
while(keepProgramRunning){

question = get_question_input();
//strip_newline(question, 100);
printf("The question: %s\n", question);
answer = get_question_answer();
//strip_newline(answer, sizeof(answer));
printf("The answer: %s\n", answer);
new_func_to_test_scope(&question, &answer);
//database_write(db, i, question, answer);
//database_print(db);

i++;

break;
}
}

void new_func_to_test_scope(char **string1, char **string2)
{
printf("These are the strings entered, how's the scope? %s, %s", *string1, *string2);
}

void strip_newline(char *string, int size)
{
int i;
printf("This is the string in the strip function: %s", string);
for(i=0; i<size;i++){
if(string[i] == '\n'){
string[i]='\0';
return;
}
}
}

void database_write(struct database *db, int id, char *question, char *answer)
{
struct quizData *quizData = &db->data[id];
printf("The id: %d\n", id);
printf("The question: %s\n", question); // Here it prints all weird stuff. Why?
printf("The answer: T%s\n", answer);
char *res = strncpy(quizData->question, question, 100);
if(!res) printf("Memory failure");
res = strncpy(quizData->answer, answer, 100);
if(!res) printf("Pointer failure");
printf("Now we should have data added %s:%s\n", db->data[id].question, db->data[i].answer);
}

void database_print(struct database *db)
{
struct database *database = db;
int i = 0;
for(i=0;i<10;i++){
printf("question: %s: answer: %s \n", database->data[i].question, database->data[i].answer);

}
}

char *get_question_input()
{
char *user_question_input;
char buffer[100];

printf("Please enter the question here: \n");
fgets(buffer, 100, stdin);
printf("This is the buffer: %s", buffer);
user_question_input = buffer;
printf("get question input: %s\n", user_question_input);

return user_question_input;
}

char *get_question_answer()
{
char *user_answer_input;
char buffer[100];
printf("Please enter the answer here: \n");
fgets(buffer, 100, stdin);
user_answer_input = buffer;
}

最佳答案

get_question_input() 返回该函数本地的 buffer[] 地址。如果在外部使用,则为未定义行为。

并强烈建议使用 -Wall 启用警告并修复所有问题。

关于c - 将字符串指针传递给 C 中的函数时得到虚假结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911419/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com