How do I write a C program to draw an equilateral triangle of any height n?

Hirya

New member
The code I used below generated 2 errors C2440: '=' : cannot convert from 'void *' to 'char *' which I can't debug

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

#define MAX_LINE_LEN 80

const char symbol = '*';
const char space = ' ';
char line[MAX_LINE_LEN];

void printChars(int,char);

int main(int argc, char *argv[]) {
int n = 0,i;
char *spaces, *symbols, *p, *q;

/* Get height */
do {
printf("Enter triangle height ( > 1 ): ");
fgets(line,MAX_LINE_LEN,stdin);
} while ((sscanf(line,"%d",&n) != 1) || (n < 2));

/* Allocate space and symbol strings */
q = spaces = calloc(n+1, sizeof(char));
memset(spaces, space, n);
symbols = calloc(n * 2 + 1, sizeof(char));
memset(symbols, symbol, n * 2);
p = symbols + (n * 2) - 1;

/* Display triangle */
for (i = 0; i < n; i++, p -= 2) {
printf("%s%s\n", q++, p);
}
free(spaces);
free(symbols);
return 0;
}
 
Back
Top