Michael Atkinson
Michael Atkinson

Reputation: 11

Segmentation Fault, Array Printing C

I am trying to write a program that takes user-input strings and prints them out as a 7 by 5 asterisk grid. I am currently trying to figure out how to print put the letters as I need to print them line by line in order for the different letters to be printed side by side.

Anyway, my attempt so far is this:

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


char capitalA[7][5] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main

    for (i = 0; i < 5 ; i++) 
    {
        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j][i]);
        }
    }
    return (0);
}//*main

My desired output is the asterisk A, but I get a Segmentation fault.

Upvotes: 0

Views: 239

Answers (5)

Hamza Jabbour
Hamza Jabbour

Reputation: 514

just use one for loop, and change 5 to 6 in the array declaration like this :

char capitalA[7][6]
//  and
for(j=0 ; j<7 ; j++)
    printf("%s\n",capitalA[j]);

your program will be like this after modification, try it ^^:

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

//edit the column nomber to support the '\0' it will be 6 not 5.
char capitalA[7][6] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main


        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j]);
        }

    return (0);
}//*main

Upvotes: 0

vsoftco
vsoftco

Reputation: 56547

char capitalA[7][5]

is not large enough. The strings literals you use in the initialization automatically include the \0 character, so you need to use

char capitalA[7][6] 

instead. And change your loops to a single one

for (j = 0; j < 7 ; j++)
    printf("%s\n", capitalA[j]);

since right now you are displaying chars using %s specifier.

If you're not happy with the '\0' being added automatically and need to save space, you can use the code you currently have or you need to initialize the array using brace initialization like

char capitalA[7][5] = {{' ', ' ', '*', ' ', ' '}, ...};

and stick to displaying the characters char by char (making sure that you use the '%c' specifier instead of the '%s').

Upvotes: 4

Mukit09
Mukit09

Reputation: 3399

 for (i = 0; i < 5 ; i++) 
 {
    for (j = 0; j < 7 ; j++) 
    {
        printf("%s\n", capitalA[j][i]);
    }
 }

You are printing char type array. Not a single character. Change it to

for (i = 0; i < 5 ; i++) 
{
    for (j = 0; j < 7 ; j++) 
    {
        printf("%c", capitalA[j][i]);
    }
    printf("\n");
}

Upvotes: 0

Spikatrix
Spikatrix

Reputation: 20244

This:

for (i = 0; i < 5 ; i++) {
    for (j = 0; j < 7 ; j++) {
        printf("%s\n", capitalA[j][i]);
}
}

should be

for (i = 0; i < 7; i++) {
    for (j = 0; j < 5; j++) {
        printf("%c", capitalA[i][j]);
    }
    printf("\n");
}

because

  1. The correct format specifier for a char is %c, not %s.
  2. You mixed up the loops. The outer one should loop 7 times while the inner one should loop 5 times.
  3. You should print a \n after the inner loop has finished executing and not in each iteration of the inner loop.

Upvotes: 2

Gopi
Gopi

Reputation: 19864

printf("%s\n", capitalA[j][i]);

should be

printf("%c", capitalA[j][i]);

What you need is to print character by character but you are trying to print a string using %s which tries to print a string until \0 is encountered.

Since %s is trying to access array out of bound you see a segmenation fault.

If you want to go with printing char by char then make sure you insert \n after each row

Upvotes: 1

Related Questions