1

I am pretty new to programming and this is a college assignment. I don't know what is causing this segmentation fault, please help.

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

main() {
    int i, n;

    struct obat {
        char nama[10];
        char kode[10];
        int harga[10];
        int stok[10];
    };

    struct obat o;

    printf("Masukan jumlah obat = ");

    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Masukan nama obat ke-%d", i + 1);
        scanf("%s", &o.nama[i]);
    }

    for (i = 0; i < n; i++) {
        printf("Nama obat ke-%d = %s", i + 1, o.nama[i]);
    }
}
1
  • probably sending in n that is more than 10? Commented May 1, 2018 at 1:08

1 Answer 1

1

scanf("%s", &o.nama[i]);

if nama is a char array, the format specifier you want is %c instead of %s. %s is for string which will try to write all of the input string (up to the nul terminator) into your char array.

So, if your input was "This Will Segfault" you would have (even from your first loop in the for loop)

o.nama[0] = T
o.nama[1] = h
o.nama[2] = i
o.nama[3] = s
o.nama[4] = "space" (not the word but the symbol)
o.nama[5] = W
o.nama[6] = i
o.nama[7] = l
o.nama[8] = l
o.nama[9] = "space" (again)
o.nama[10] = S //This is the seg fault most likely, although it may also write into other parts of your struct unintentionally.

if you want an array of strings instead of an array of chars you need to change you struct to something like this:

main() {
    int i, n;

    struct obat {
        char nama[10][512]; //the 512 here should be #defined
        char kode[10];
        int harga[10];
        int stok[10];
    };

    struct obat o;
    memset(o, 0, sizeof(stuct obat)); //set your struct to 0 values, important for strings.

    printf("Masukan jumlah obat = ");

    scanf("%d", &n);

    for (i = 0; i < n; i++) { //loops over at most 10 inputs and reads the input string
        printf("Masukan nama obat ke-%d", i + 1);
        scanf("%s", &o.nama[i][0]);
    }

    for (i = 0; i < n; i++) {
        printf("Nama obat ke-%d = %s", i + 1, o.nama[i][0]);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

but i'm using string, i also included string.h
but when you access o.nama[i] you get the ith character in the array, and not a string. if you want an array of strings you need to change your struct to something like char nama[10][MAX_INPUT_SIZE] and define MAX_INPUT_SIZE to be something that wont be smaller than your input strings (512 bytes is plenty most likely). Then when you read it in use &o.nama[i][0] to read in the ith string. Is this what youre trying to do with this program?
what is ith character? also yes im trying to use string and thx for the advise, i'll try that and let you know if that work
I edited the post to include a string implementation. The "ith" element is the element in the string with the i offset, o.nama[i], which is *(o.nama + i).
so i just tried [MAX_INPUT_SIZE] and it actually work, thank you so much, you're life saver, this assignment is due today, thank you for you help sir/ma'am.
|

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.