user2958542
user2958542

Reputation: 341

printing 2D character array

I keep getting a nest functions are forbidden error. When I try to use a pointer as well I receive and error reporting that "initialization makes pointer from integer without a cast." and unsure what's going on with that. (using -ansi and -pedantic )

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


void createArt(int m) {
int i = 0, j =0, k = 1, l = 1;

char ascii_art[5][5] {
    {'/','/','/','/','/'},
    {'/','/','/','/','/'},
    {'/','/','/','/','/'},
    {'/','/','/','/','/'},
    {'/','/','/','/','/'}
};  

for(i; i < (5 * m); i++) {
    for(j; j < (5 * m); j++) {
        printf("%s", ascii_art[i][j]); /* can't print out chars */
    }
}
}

int main() {
int multiplier = 0, m = 1;

printf("Enter a number: ");
scanf("%d", &multiplier);

createArt(multiplier);

return 0;
}

Upvotes: 0

Views: 55

Answers (2)

Dabo
Dabo

Reputation: 2373

Issues that i found so far

1)You missing = after declaration ascii_art

2) You should replace %s with %c in

 printf("%s", ascii_art[i][j]); 

%s is for printing string terminated with \0, and for single character you need to use %c

Also i'm afraid you going to do something bad with that multiplier since your loop stopping condition is i< 5*m and m obtained from user with scanf. I see only two valid options for multiplier in this case 0, 1 any other value will take out of your array.

Upvotes: 0

user3072164
user3072164

Reputation:

There should be a = after char ascii_art[5][5] and chars are printed with %c, not %s.

I cannot reproduce the initialization makes pointer from integer without a cast error, but you have several unused variables and the first i in for(i; i < (5 * m); i++) { has no effect. You can leave it out as well. (Same for the other loop).

At least in the second loop (first one wouldn't hurt either) you probably want to write j=0, otherwise the inner loop will only be executed for i=0.

Your code will also fail if multiplier is greater than 1, because then i and j will become greater than 5 and you will try to access the array out-of-bounds.

Upvotes: 3

Related Questions