1

I started learning C and I wrote a short program implementing structures. For now, it only has a structure and a short function that is supposed to fill one of the structures (several are stored in an array). Program gets all the values (currently only for one structure to help me see what's going on) and the problem starts in main, in

printf("%s", tablica[0].nazwa);

Because program stops responding (no errors or warnings before).

If I put:

printf("%d", tablica[0].x);

It will print the value I put as x, so I know there is some problem with string in printf (but I can't figure out why). It's probably easy, but I'm just a beginner.

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

struct struktura
{
    char *nazwa;
    double x, y, z;
};

int wczytaj(struct struktura tab[])
{
    int i;
    for ( i = 0; i<1; i++)
        {
        printf("Podaj nazwe: ");
        scanf("%s", &tab[i].nazwa);
        printf("Podaj x: ");
        scanf("%i", &tab[i].x);
        printf("Podaj y: ");
        scanf("%i", &tab[i].y);
        printf("Podaj z: ");
        scanf("%i", &tab[i].z);
        };
    return 0;
}

int main(struct struktura* a)
{   int i;
    struct struktura tablica[6];
    int wyniki[6][6];
    wczytaj(tablica);
    printf("%s", tablica[0].nazwa);
}

Sorry for some names being in Polish, I can correct that, but I hope it doesn't blur the program).

6
  • 1
    Since nazwa is a char *, &tab[i].nazwa is a char **. Since you pass scanf the address you want it to store something in, you are asking it to store a string in the space of a pointer to a character. What if the string takes up more space than a pointer to a character takes? (Which it likely does.) Somewhere you have to allocate enough space to hold the string. Commented Jan 6, 2017 at 19:24
  • 1
    It looks like you don't ever allocate any space for nazwa (defined as a char *) so it is a null pointer. Commented Jan 6, 2017 at 19:25
  • 1
    For starters don't mess with malloc, but define it statically like char nazwa[30] Commented Jan 6, 2017 at 19:25
  • 1
    int main(struct struktura* a) that isn't the proper signature for main(). Commented Jan 6, 2017 at 19:30
  • @DavidSchwartz But he does try to use it when the printf statement is executed. Commented Jan 6, 2017 at 19:40

1 Answer 1

1

You are using the wrong format specifiers for double types in

scanf("%i", &tab[i].x);
printf("%d", tablica[0].x);

and others. They should be

scanf("%lf", &tab[i].x);
printf("%f", tablica[0].x);

Also this string input

scanf("%s", &tab[i].nazwa);

should lose the & ampersand like this

scanf("%s", tab[i].nazwa);

but even so nazwa has no memory allocated to it. As suggested in comments you could get going with a fixed array like

struct struktura
{
    char nazwa[30];
    double x, y, z;
};

You have a very unsual signature for main, which is usually

int main(void)

or

int main(int argc, char *argv[])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.