Running a program while scanning?

main.c (with all headers like stdio, stdlib, etc.):

int main()
{
int input;

while(1)
{
    printf("\n");
    printf("\n1. Add new node");
    printf("\n2. Delete existing node");
    printf("\n3. Print all data");
    printf("\n4. Exit");
    printf("Enter your option -> ");
    scanf("%d", &input);

    string key = "";
    string tempKey = "";
    string tempValue = "";
    Node newNode;
    Node temp;
    switch (input) {
        case 1:
            printf("\nEnter a key: ");
            scanf("%s", tempKey);
            printf("\nEnter a value: ");
            scanf("%s", tempValue);          //execution ternimates here

            newNode.key = tempKey;
            newNode.value = tempValue;

            AddNode(newNode);
            break;
        case 2:
            printf("\nEnter the key of the node: ");
            scanf("%s", key);
            temp = GetNode(key);
            DeleteNode(temp);
            break;
        case 3:
            printf("\n");
            PrintAllNodes();
            break;
        case 4:
            exit(0);
            break;
        default:
            printf("\nWrong option chosen!\n");
            break;
    }
}

return 0;
}

      

storage.h:

#ifndef DATABASEIO_H_
#define DATABASEIO_H_

//typedefs
typedef char *string;

/*
 * main struct with key, value,
 * and pointer to next struct
 * Also typedefs Node and NodePtr
 */
typedef struct Node {
    string key;
string value;
struct Node *next;
} Node, *NodePtr;

//Function Prototypes
void AddNode(Node node);
void DeleteNode(Node node);
Node GetNode(string key);
void PrintAllNodes();

#endif /* DATABASEIO_H_ */

      

I am using Eclipse CDT and when I enter 1 I was typing in the key. Then the console says. I used gdb and got this error:

Program received signal SIGSEGV, Segmentation fault.
0x00177024 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6

      

Any ideas why?

+2


a source to share


4 answers


you must allocate enough memory for your strings (typedef char * string) to be read with scanf ()



+5


a source


  • As Jonathan and Patrick said, First allocate memory and then pass pointers / arrays to scanf.

//Change the value here to change the size of Ur strings (char-arrays actually)


#define MAX_LENGTH 20

char key[MAX_LENGTH];

char tempKey[MAX_LENGTH];

char tempValue[MAX_LENGTH];

You can also check out the little gdb step-by-step tutorial I have here:



2600Hertz / Hacking-in-any-executable-using-gdb /

GOODLUCK !!

CVS @ 2600Hertz

+3


a source


You must allocate all the storage scanf()

to use before calling it; scanf()

does not allocate memory. Your lines are empty; there is minimal storage for values. Entering any additional data is a disaster.

In addition, scanf () expects a pointer to a character, not a reference to a string or value - since it is a variable function, there are only a limited number of warnings the compiler can make.

+2


a source


Hmm, are you sure scanf can use the supplied string to store data?

I would try to use a char buffer that is large enough, or switch to real C ++ functions for reading input.

0


a source







All Articles