JMBTaylor
JMBTaylor

Reputation: 33

Structure causing program to crash

int get_name()
{
    char cName[] = "hello";
    int iCode, i = 0;
    struct sign_in items[6];//array of six structure variables

    Fpointin =fopen("namepass.txt","r");

    if (Fpointin == NULL)
    {
        printf ("File does not exist.\n");
    }
    else
    {
        for (i=0;i<6;i++)
        {
            fscanf(Fpointin,"%s %d",items[i].name,items[i].password);//read all values from the file into th structure
        }
        printf("Here is the sign_in structure\n");//print the entirety of the sign_in structure
        for (i=0;i<6;i++)
        {
            printf("name: %s\ncode: %d\n\n", items[i].name, items[i].password);
        }
    }
    fclose(Fpointin);
}

Hey all. So I've got this section of code from a project and it crashes whenever I try to run it. I'm trying to read names and their respective pass codes from a file to a structure and its not working. In the fscanf line I had the %s %d identifiers swapped and it ran but it printed random stuff that wasn't even close what was in the file. Any ideas?


[update from comment:]

struct sign_in
{ 
  int password; //The password for each player 
  char name[]; //Name of the people who can sign in
}

Upvotes: 0

Views: 502

Answers (4)

SDxBacon
SDxBacon

Reputation: 96

First,you should not use char name[] in your structure. Because array should declear it's memory size before you use it.

So, you can change char name[] to:

  1. char *name : That you have to allocate a memory size for it. check my code below.
  2. char name[NAME_SIZE]

Second, if you want to change the value inside another function you should pass its' memory address to it. for example:

Example 1: if you just pass the value but not address

void foo(int in)
{
    in = 5;
}

int main(int argc, char const *argv[])
{
    /* code */
    int a = 10;
    foo(a);
    printf("after foo, a: %d\n", a);
    return 0;
}

output: after foo, a: 10

Example 2: if you pass the memory address.

void foo(int *in)
{
    *in = 5;
}

int main(int argc, char const *argv[])
{
    /* code */
    int a = 10;
    foo(&a);
    printf("after foo, a: %d\n", a);
    return 0;
}

output: after foo, a: 5

So, you have to pass items[i].password's memory address to fscanf like:
fscanf(Fpointin,"%s %d",items[i].name, &items[i].password);

Hence,your code should looks like:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct sign_in {
    int password;//The password for each player
    char *name;//Name of the people who can sign in
};

void init_myStruct(struct sign_in *s_in) {
    s_in->name = calloc(1, sizeof(char)*1024);
    return;
}

void destroy_myStruct (struct sign_in *s_in) {
    free(s_in->name);
    s_in->name = NULL;
    return;
}

int get_name()
{
    int i = 0;
    FILE *Fpointin = NULL;
    struct sign_in items[6];//array of six structure variables

    for ( i = 0; i < 6; i++)
        init_myStruct(&items[i]);

    Fpointin =fopen("namepass.txt","r");
    if (Fpointin == NULL) {
        printf ("File does not exist.\n");
        goto end_of_use;
    }
    else
    {
        printf("Here is the sign_in structure\n");//print the entirety of the sign_in structure
        for ( i = 0; i < 6; i++) {
            fscanf(Fpointin,"%s %d",items[i].name, &items[i].password);//read all values from the file into th structure
            printf("name: %s\ncode: %d\n\n", items[i].name, items[i].password);
        }
    }
    fclose(Fpointin);

end_of_use:
    for ( i = 0; i < 6; i++)
        destroy_myStruct(&items[i]);
    return;
}

int main(int argc, char const *argv[])
{
    get_name();
    return 0;
}

Upvotes: 1

alk
alk

Reputation: 70883

The char name[]; in

struct sign_in
{
  ...
  char name[]; 

is of an incomplete type. It does not allocated memory.

Use

#define NAME_LEN_MAX 42

...

struct sign_in
{
  ...
  char name[NAME_LEN_MAX + 1];

for example and adjust the scanning like this:

fscanf(Fpointin, "%42s %d", items[i].name, &items[i].password);

Upvotes: 1

ecsrid
ecsrid

Reputation: 29

It can be of type char *, and you you may not have allocated space for it. It will then definitely crash

Upvotes: 0

Furquan
Furquan

Reputation: 678

It would be really helpful to see what the structure sign_in looks like. But, from a quick look of the code, one obvious error is the way password is being scanned.

fscanf(Fpointin,"%s %d",items[i].name,items[i].password);

This line should be:

fscanf(Fpointin,"%s %d",items[i].name, &items[i].password);

You need to pass in the address of the variable items[i].password so that fscanf can store a value in the memory location pointed by that address.

Hope that helps.

Upvotes: 2

Related Questions