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

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    8

    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?

    Code:
    #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;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    It means that you are compiling as C++ instead of C.

    C++ will complain about the malloc returning void* but being assigned to a char*. You need a cast to shut it up: (char*)malloc(n + 1)

    C allows you to assign a void* to another pointer type without a cast, so if you were compiling as C it wouldn't complain.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    @john.c
    My previous post doesn't seem to have got through.
    Thanks very much for showing where I was going wrong. I thought I was compiling in C using GCC, but clearly I was compiling in g++. I've tried it out in Pelles C and it works fine.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    GCC is perfectly capable of compiling C code, providing you tell it that you are compiling C.

    Using the GNU Compiler Collection (GCC): Invoking G++
    You need to be somewhat careful about what you call your source files.
    GCC will think .C files are C++ source files, and .c files are C source files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    Say RNBW, I'm kind of bi-lingual, or tri-lingual, or whatever, as I've coded in all kinds of versions of basics, C, and C++ for like 30 years. If you really like basic like string handling, you shouldn't be messing with C. I know this question is in the C forum, but C++ String Classes are the way to go. And by saying that I don't want to denigrate C in any way. Truth be told, I'm more of a C coder than a C++ coder myself, but I'm just telling you the way it is. String handling in C is essentially nothing more than allocating memory and using specialized memory move operations to move bytes around from one place to another, which bytes just happen to be terminated by a null character. In a sematic sense, they aren't 'strings' - just bytes you are moving from one place to another.

    Another aspect of this issue, relating specifically to versions of basic, is that the designers of the C++ String Class in the C++ Standard Library didn't create a String Class with much resemblence to basic's string handling. They could have created it any way they wanted to, but what they eventually settled on looks very alien to a basic coder learning C++. In my case, when I was first teaching myself C++ many years ago, I used the creation of my own String Class tailored after Basic like String handling as a learning exercise for myself. As a matter of fact, the very operation you first mentioned is in my String Class. I'm just suggesting this as a possible learning exercise for you. Its very, very doable!

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    @freddie
    I have a BASIC programming background. I am trying to expand my programming knowledge now that I have retired and have a little more time. I am looking at C, C++ and Free Pascal at the moment. I don't want to get into arguments about which is the best programming route to take. That is an endless and futile argument.

    I have coded the program in C++ and now want to do it in C. I will then do it in Free Pascal.

    The point was that I just wanted to print a row of "=" on the screen. It is a doddle to do it in BASIC and C++. Not so easy in C, although @Repeater produced a simple bit of code, which I will gratefully use.

    I am still a beginner at C and C++, but I'm learning all the time and I'm grateful for all the help I can get from tutorials, books and forums.

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    @Salem
    You've hit on my mistake.
    I assumed that GCC would automatically pick on a C or C++ program from its .c or .cpp extension. I realise now that this is not the case and will make sure I don't fall into that trap again.
    Thank you for your explanation.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Stop putting @username if you don't want to keep being moderated.
    This isn't tw*****
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    8
    Stop putting @username if you don't want to keep being moderated.
    Thanks for the advice. I didn't realise that would happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing "Struct Char" with "Char in Char Array"
    By JonathanS in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2012, 03:06 PM
  2. "strcpy(string,char[int])" doesn't work?
    By JonathanS in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 10:32 AM
  3. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM
  4. conversion from char string e.q. "AA" to UTF-8 format
    By quickNitin in forum C Programming
    Replies: 10
    Last Post: 09-07-2006, 09:01 AM
  5. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM

Tags for this Thread