Sama Azari Sama Azari - 4 months ago 13
C Question

Printing values of array in C

I have a problem on printing the arrays content. Every time I want to print them I get a segmentation fault(look at the commented code). Why is that?

#include <stdio.h>
int main(int argc, char* argv[])
{
char const* const fileName = argv[1];
FILE* file = fopen(fileName, "r");
char line[256];
char str1[10], str3[10];
int str2[10], str4[10];
while (fgets(line, sizeof(line), file)) {
printf("The Full Line is: %s", line);
sscanf(line,"%s -> %s", str1,str3);
printf("First is: %s \n", str1);
printf("Thirs is : %s\n", str3);
}
fclose(file);
// printf("%s", str1[0]);
// for (int i=0; i<4; i++){
// printf("This is it!!! %s ", str1[i]);
// }
return 0;
}


The input text file contains:

main+0x20 -> main+0x10
function1 -> function2+0x20
function2+0x34 -> function3


or more complex one:

Function2+0x22 -> main+0x92
main -> main+0x22
Function2 -> vuln+0x12
main+0x86 -> Function1
main+0x86 -> main+0x92
Function1+0x12 -> Function2+0x22
func1+0x10 -> main+0x76
dummya -> func1+0x10
main+0x6a -> main+0x76
main+0x6a -> func1
main+0x76 -> main+0x86
main+0x22 -> main+0x3a
main+0xa2 -> main+0xae
func1 -> dummya
func1 -> func1+0x10
main+0x92 -> main+0xa2
main+0x3a -> main+0x52
main+0x52 -> main+0x6a


Can I put this values into a 2D array. And then print it. If yes how to do it?

I want an output like this:

Array1[1]= main
Array2[1]=0x20 //in int
Array3[1]=main
Array4[1]=0x10 //in int

Array1[2]=Function1
Array2[2]=0 //in int
Array3[2]=Function2
Array4[2]=0x20 //in int

Array1[3]=Function2
Array2[3]=0x34 //int
Array3[3]=Function3
Array4[3]=0
//Continue until EOF

Answer

Use of %s assume pointer to char array ending null symbol. But you give char instead pointer to char.

If you need one symbol - you should use %c:

printf("%c", str1[0]);
for (int i=0; i<4; i++){
    printf("This is it!!! %c ", str1[i]);
}

If you need string with offset - you should use pointers:

printf("%s", &str1[0]);
for (int i=0; i<4; i++){
    printf("This is it!!! %s ", &str1[i]);
}

See printf reference.

EDIT1:

Solved...

#include <stdio.h>
int main(int argc, char* argv[])
{
    char const* const fileName = argv[1];
    FILE* file = fopen(fileName, "r");
    char line[256];
    char i, cnt = 0;
    char str1[3][10], str3[3][10], * tmp;
    int arr2[3], arr4[3];
    while (fgets(line, sizeof(line), file)) {
        tmp = strstr(line," -> ");
        arr2[cnt] = 0; arr4[cnt] = 0;
        sscanf(line,"%[^+ ]%x%*s", str1[cnt],&arr2[cnt]);
        sscanf(tmp," -> %[^+ ]%x%*s", str3[cnt],&arr4[cnt]);
        cnt++;
    }
    fclose(file);
    for (i = 0; i < cnt; i++) {
        printf("Array1[%d] %s \n", i+1, str1[i]);
        printf("Array2[%d] %x \n", i+1, arr2[i]);
        printf("Array3[%d] %s \n", i+1, str3[i]);
        printf("Array4[%d] %x \n", i+1, arr4[i]);
    }
    return 0;
}

EDIT2:

It is strictly recomended to read chux's answer and c/c++ reference for understand what you doing and what the hell is going after compiling.