将字符串指针传递给C中的函数时获取伪造的结果

时间:2014-04-07 11:42:09

标签: c arrays pointers

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

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

  2. 然后在主要部分打印就好了。

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

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

  5. 最后,我原来的问题,当传递*问题和*回答char指针到函数database_write时,它会打印所有伪造的。

  6. 我怀疑这是一个范围问题,但我现在花了很长时间才研究它并且无法弄清楚如何。因此,一点帮助将受到高度赞赏。

    #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;
    }
    

1 个答案:

答案 0 :(得分:2)

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

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