concatenating a string

This is a discussion on concatenating a string within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to make a string lenghthier by 1 char using the following: Code: void printChar(char* C){ int i=0; ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    concatenating a string

    Hello,

    I'm trying to make a string lenghthier by 1 char using the following:

    Code:
    void printChar(char* C){
    	int i=0;
    	char ci = *(C+i);
    	while (ci != '\0'){
    		printf(&ci);
    		i++;
    		ci = *(C+i);
    	}
    }
    char* updateChar(char *C, char c){
    	int i=0;
    	char ci = *(C+i);
    	while (ci != '\0'){
    		i++;
    		ci = *(C+i);
    	} int j = i++;
    	*(C+j) = (char*) malloc(sizeof(char*));
    	*(C + i) = c;
    	*(C+(++i)) = '\0';
    	return C;
    }
    The output of
    Code:
    	char C[] = "Ciao";
    	*C = updateChar(C,'o');
    	printChar(C);
    is: Wiao∞o !

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm not sure why you want to do this this way, but here are the problems I found:
    Code:
    *(C+j) = (char*) malloc(sizeof(char*));
    You are not adding a pointer, you are adding a single char, so get rid of the asterisks.
    Code:
    *C = updateChar(C,'o');
    updateChar operates on C so you do not need (and in this case should not) use the return value. (Just call the function, "updateChar(C,'o');")
    Code:
    } int j = i++;
    you've gone too far. Remove the ++, make those other changes, and the program will print out "Ciaoo"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    you've gone too far. Remove the ++, make those other changes, and the program will print out "Ciaoo"
    Only once the printChar function is fixed.

    Why not just do:
    Code:
    printf("%s", C);
    to print out your string?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    updateChar operates on C so you do not need (and in this case should not) use the return value. (Just call the function, "updateChar(C,'o');")
    Not really, since updateChar used without a return value couldn't actually change the size of the array unless you change the values you pass it. Therefore, using the parameters it currently has, there is no way to make that string actually longer without returning a new string. Furthermore, with its current prototype, unless you free the old string outside of the function, you're going to make a memory leak.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    Not really
    In fact really, since I compiled the code with the changes I mentioned (no return value), used printf back in main, and C is now "Ciaoo".

    However, I don't think a sane person should write code this way anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    In fact really, since I compiled the code with the changes I mentioned (no return value), used printf back in main, and C is now "Ciaoo"
    Honestly, that doesn't mean a damn thing.
    Code:
    #include<stdio.h>
    int main( void )
    {
        char buf[ 2 ];
        buf[ 0 ] = 'H';
        buf[ 1 ] = 'i';
        buf[ 2 ] = '!';
        buf[ 3 ] = '\0';
    
        printf( "%s\n", buf );
        return 0;
    }
    This will almost NEVER crash. It doesn't make the code good, correct, or right in any way shape or form.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, I totally agree. Like I said, no one should be writing code like this anyhow, I presume it is some kind of experiment.

    I had thought you meant that without the return value, "updateChar" COULD NOT affect "C" globally, which is another issue but not part of this thread.

    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No I meant this:
    Code:
    void myfailuremalloc( char * s, size_t t )
    {
        s = malloc( t );
    }
    This won't actually make s point to the string malloc is making once this function ends. Which is what they're trying to do...


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    No I meant this
    Ah! Well so That Issue (qv.) is part of this thread then. You're right. However, I noticed testing the OP that using the return value with the original "C" dereferenced caused more some unwelldefined problems. But I shouldn't have recommended what I did considering the malloc() in updateChar. Sorry sorry sorry. A few days of .pl and .js and I just lose my head.

    IMO, even if you didn't want to use some standard C functions to do this stuff, this whole approach is even worse than the "worst" scenario I might have dreamt up as humour, I have no idea how this person got on this track or why they think it is even somewhere to stay temporarily. We are for sure ignoring some compiler warnings, methinks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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