user1922524
user1922524

Reputation: 1

Getting bogus results when passing a string pointer to a function in C

I am currently trying to learn C, and have encountered a few issues trying to work with strings. The problem is as follows:

  1. I retrieve a string pointer from the function get_question_answer from the user, and sets it to the pointer char *question,

  2. which then in the main part prints just fine.

  3. But then when I pass it to any function I get weird results.

  4. In the new_func_to_test_scope function it prints most of the input (16 chars), then adds a ¿.

  5. And lastly, my original problem, when passing the *question and *answer char pointers to the function database_write it prints all bogus.

I suspect that this is a scope problem, but I have spent quite a little while on it now and not having been able to figure out how. Therefore a little help would be most highly appreciated.

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

Upvotes: 0

Views: 108

Answers (1)

Jeyaram
Jeyaram

Reputation: 9474

get_question_input() returns address of buffer[] which is local to that function. If it is used outside, then it is undefined behaviour.

And Strongly suggest to enable warning with -Wall and fix all.

Upvotes: 2

Related Questions