C equivalent of BASIC's STRING$(80,"char")

I am looking for the equivalent in C of BASIC's STRING$(80,"char") ie printing out a string of 80 same characters aaaaaaaa...etc

I found the following code in Rosetta Code, but it comes up with an error
"invalid conversion from 'void*' to 'char*' [-fpermissive].

I have no idea what this means. Can someone enlighten me please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
char * char_repeat( int n, char c ) {
  char * dest = malloc(n+1);
  memset(dest, c, n);
  dest[n] = '\0';
  return dest;
}
 
int main() {
  char * result = char_repeat(5, '*');
  puts(result);
  free(result);
  return 0;
}
Last edited on
I am looking for the equivalent in C of BASIC's STRING$(80,"char") ie printing out a string of 80 same characters aaaaaaaa...etc


Can't go with C++?

1
2
3
4
5
6
7
#include <string>
#include <iostream>

int main()
{
  std::cout << std::string(80, 'a');
}
Last edited on
@Repeater
No, I want it to be in C. I tried it first using the C++ version, but it doesn't work with C.
I believe this will do it.

char same80[81] = {0};
memset(same80, 'a',80);

which is what you had without the pointers.

if you insist on pointers,
what you have above is correct as far as I can tell. what does it do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

char * char_repeat( int n, char c )  //a subroutine that takes how many and what char and returns the "string"
{ 
  char * dest = malloc(n+1); //allocate N+1 memory locations; the +1 is for the special null terminal (c-strings end in the special ascii value zero)
  memset(dest, c, n); //set ALL the locations from 0 to n as the value in c/ 
  dest[n] = '\0'; //append the special zero terminal character
  return dest; //return the complete string. 
}
 
int main() {  //a main program that exercises the above subroutine.  
  char * result = char_repeat(5, '*');
  char * a80 = char_repeat(80, 'a'); //you want this.
  puts(result);
  free(result);  //give back the memory to the OS.  Modern OS do this for you anyway on program exit, which is up next, but its best practice to do it yourself, and critical to do it yourself in large programs that need to use the space for something else before the program ends. 
  return 0;
}



if you are using a c++ compiler you MUST cast the result of malloc. If its a true C compiler, I don't think it cares (cant remember). It won't hurt in C...
char * dest = (char*) malloc(... //the cast is the (char*)

malloc returns a void *
Last edited on
I can't see why you need to use malloc juist to print the same character n times.
I can't see why you need to use malloc juist to print the same character n times.


Indeed. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
 
void repeat(char c, int n)
{
for (int i = 0 ; i < n ; i++)
  {
    printf("%c", c);
  }
}
 
int main(void) 
{
	repeat('a', 80);
	return 0;
}
Thank you all for your help. I thought I was compiling using GCC, but it appears I was using g++ , so trying to compile as C++. It didn't like it. It does work using Pelles C.
To compile as C, at the command line enter "gcc" instead of "g++".

g++ is just a convenience to call gcc with the language specified as C++, whereas calling gcc will leave it at the default language of C.
Last edited on
@Repeater
I can't see why you need to use malloc just to print the same character n times.


I tried your code and it does the job perfectly. It shows simple is always best!
Thank you.
Topic archived. No new replies allowed.