Copy every single character from a char into another char.

This is a discussion on Copy every single character from a char into another char. within the C Programming forums, part of the General Programming Boards category; Hey guys, I have some problems with copying a single character from one char into another. How do I do ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    5

    Copy every single character from a char into another char.

    Hey guys,

    I have some problems with copying a single character from one char into another. How do I do that? I'm quite the beginner, so I would appreciate your help.

    Here is the function I am using
    Code:
    void
    text_col (char line[])
    {
        char line1[1];
        int l, ii;
    
         l = strlen(line);
    
        //looping through the word - each character
        for (ii=0;ii<l;ii++){
    
        //This is my attempt to copy a character
         strcpy (line1[0], line[ii]);
    
        }
        
    
    
    }
    I am trying to copy in a loop every single character from char line into char line1. That is not working.
    Can you tell me why and how I can fix it?

    Thank you very much!
    Last edited by Roscoe Araujo; 06-05-2013 at 10:16 AM.

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    210
    Quote Originally Posted by Roscoe Araujo View Post
    Hey guys,

    I have some problems with copying a single character from one char into another. How do I do that? I'm quite the beginner, so I would qppreciate your help.

    Here is the function I am using
    Code:
    void
    text_col (SDL_Surface *monitor, TTF_Font *font, int color, char line[], int x, int y, int time)
    {
        char line1[1];
        int l, ii;
       
        //Buchstaben werden der Reihenfolge nach gemappt
        for (ii=0;ii<l;ii++){
    
        //Hier soll jeder einzelne Buchstabe aus line in line1 kopiert werden
         strcpy (line1[0], line[ii]);
    
        }
        
    }
    I am trying to copy in a loop every single character from char line into char line1. That is not working.
    Can you tell me why and how I can fix it?

    Thank you very much!
    It looks like you are confusing char with arrays of char and NUL terminated array of chars (strings).

    In your example, the line[] argument denotes an array of chars of unknown size . The line1[1] local variable is defined as an array having room for exactly 1 char (so it could just be defined as a single char variable).

    Moreover, in your loop you are using l uninitialized, which means it can contain pretty much anything in there (thus making it totally unreliable for controlling the loop iterations).

    Al in all, it seems like you should backup and study the core C language, instead of diving directly into SDL.

    In any case, if line[] is a valid cstring (that means, it contains a terminating '\0' character) the you can use...

    Code:
    size_t len = strlen(line);
    in order to determine line's current length. Then you could use len for allocating enough memory for line1 and copy line's contents into it...

    Code:
    char *line1 = malloc(len+1);
    if ( NULL == line1) {
        /* malloc() failed, handle the error here */
    }
    else {
        /* do whatever you want to do with line1 here, and then... */
        free( line1 );    /* free memory allocated for line1 */
    }
    EDIT:

    The quote I used is the original posted by OP, before he/she editing the original post (thus the SDL reference in my answer ;-) ).
    Last edited by migf1; 06-05-2013 at 10:15 AM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Thank you for your reply. I corrected my code (I wanted to cut it down to the central information, l contains the length of line).
    How do I exactly only the first character from line into line1?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Las Vegas, NV
    Posts
    10
    This will work:

    Code:
    *(&line1[0]) = line[ii];
    I have to ask, why does the destination need to be a single-element array rather than just a char variable?

    You also need to make sure the "l" variable is initialized to the length of "line".

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Because I only need a single character in line1.

  6. #6
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    210
    Quote Originally Posted by Roscoe Araujo View Post
    Thank you for your reply. I corrected my code (I wanted to cut it down to the central information, l contains the length of line).
    How do I exactly only the first character from line into line1?
    If you want line1 to be just a char with just the 1st character of line, then you can...

    Code:
    char line1 = 0;
    if ( line )
       line1 = *line;    /* or: line1 = line[0] */

  7. #7
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    210
    Quote Originally Posted by Roscoe Araujo View Post
    Because I only need a single character in line1.
    So, you want a loop traversing all characters of line[] and assign them one by one to line1, at each iteration?
    If so, then perhaps something like the following will suit you...

    Code:
    void text_col( char line[] )
    {
        char line1 = 0;
        int i;
    
        for (i=0; '\0' != line[i]; i++) {
            line1 = line[i];
            ...
        }
    }

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Thank you very much :-) I tried something similar, it did not work, that is why I am looking for helo in this forum. I will try it again tomorrow mornng,

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    I have found a solution (of course with some help ):

    Code:
    const char *p = NULL;
    char c;
    char a[2] = { '\0', '\0' };
    
    c = line[ii];       putc(c, stdout);    putc(' ', stdout);
    a[0] = line[ii];    fputs(a, stdout);   putc(' ', stdout);
    p = &line[ii];      fputs(p, stdout);   putc('\n', stdout);
    If I add this in the loop of my function, it works.
    This seems to have done the magic.

    Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. replace single character with matrix of char
    By rob90 in forum C Programming
    Replies: 7
    Last Post: 12-28-2009, 02:19 PM
  2. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  3. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 11:33 PM
  4. strcpy - How to copy a single character to string?
    By rockysfr in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 03:12 AM
  5. Single char to/from x/y?
    By Sebastiani in forum C Programming
    Replies: 6
    Last Post: 06-03-2002, 10:40 AM

Tags for this Thread


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