Thread: using putchar()

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    78

    using putchar()

    hey all again =]

    I'm having problems with the putchar() function, here is the code:

    Code:
    putchar(chara1[i] + chara1[i-1]);
    error: invalid operands to binary + (have 'char *' and 'char *')|

    any help?

  2. #2
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    I'm not sure if its the right function to use, basically:

    so, the function strcat() puts a char behind a string
    is there a function that puts a char in front of a string

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    If chara1 is an array of strings, then you're trying to add two pointers, which doesn't make any sense (C doesn't support string concatenation using the "+" operator like some languages).

    putchar writes a char to stdout. That doesn't seem like the function you want.

    strcat concatenates a string to the end of another string. If you want to "put" a character in front of a string, you'll need to move all characters in the string forward and then set the first character to the new character:

    Code:
    // untested
    // s is the string, c is the char
    // make sure the buffer holding the string s is long enough to hold another character before doing this:
    memmove(s + 1, s, strlen(s) + 1); // +1 for the terminator
    s[0] = c;

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by christop View Post
    If chara1 is an array of strings, then you're trying to add two pointers, which doesn't make any sense (C doesn't support string concatenation using the "+" operator like some languages).

    putchar writes a char to stdout. That doesn't seem like the function you want.

    strcat concatenates a string to the end of another string. If you want to "put" a character in front of a string, you'll need to move all characters in the string forward and then set the first character to the new character:

    Code:
    // untested
    // s is the string, c is the char
    // make sure the buffer holding the string s is long enough to hold another character before doing this:
    memmove(s + 1, s, strlen(s) + 1); // +1 for the terminator
    s[0] = c;
    You have no error checking for the length of the string and the size of the array.

    A better way of doing this, providing some error checking in the actual code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char ch = 'A';
      char src[64] = "This is a test.";
      char dest[128] = "";
    
      sprintf(dest, "%c%s", ch, src);
      printf("Final: [%s]\n", dest);
    
      return 0;
    }
    After prepending the character to the string, the combined dest string could be copied back to the src array, using strcpy()

    I consider this safer considering the level of experience of the O/P.
    Last edited by rstanley; 1 Week Ago at 01:39 PM.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by rstanley View Post
    You have no error checking for the length of the string and the size of the array.
    Correct. I left it as an exercise to the reader. (I even left a comment to that effect above the memmove.)

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    its maybe easier to show you what im doing =]

    the current output is:

    0
    01
    012
    0123
    01234
    012345
    0123456
    01234567
    012345678
    0123456789

    which is correct

    the problem is, I am trying to find a way to assign the array in the right order without having to use the strrev() function

    any help?

    Code:
        int i;
    
    
        int int1[10];
        char chara1[11][11] = {0};
        char chara2[10][10];
        
    
    
        for(i=0;i<10;i++){
    
    
            int1[i] = i; 
            itoa(int1[i],chara1[i],10);//convert number from int to string
    
    
    
    
            strcat(chara1[i],chara1[i-1]);//Concatenates from previous array number
            strcpy(chara2[i],chara1[i]);//copying array otherwise it would get scrammbled
            strrev(chara2[i]);//reverses string because 'strcat(chara1[i],chara1[i-1]);' puts the char behind the string instead of infront
    
    
    
    
            printf("%s\n",chara2[i]);
    
    
    
    
        }
    Last edited by WaterSerpentM; 1 Week Ago at 03:34 PM.

  7. #7
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    wow

    Code:
    strrev(strcpy(chara2[i],chara1[i]));
    actually works lol

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by WaterSerpentM View Post
    wow

    Code:
    strrev(strcpy(chara2[i],chara1[i]));
    actually works lol
    Yep, strcpy returns a pointer to the destination string, so it's the same as calling strcpy and strrev separately with the same destination (and there's no performance benefit to doing it one way or the other, maybe unless you have all compiler optimizations disabled or you're using a really old or bad compiler).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putchar
    By jandayan24 in forum C Programming
    Replies: 3
    Last Post: 12-20-2011, 11:24 AM
  2. Something odd with putchar()...
    By Hakins90 in forum C Programming
    Replies: 7
    Last Post: 12-27-2007, 04:36 AM
  3. putchar
    By cogeek in forum C Programming
    Replies: 21
    Last Post: 11-20-2004, 10:52 AM
  4. Putchar
    By Inferno in forum C Programming
    Replies: 4
    Last Post: 08-13-2004, 09:08 PM
  5. putchar and % (mod)
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-09-2001, 01:14 PM

Tags for this Thread