Thread: strcat() problems, maybe

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    strcat() problems, maybe

    When I try to cocatenate a character in a buffer to my string, or character array, it gives me the warning. IT is the line where I am using strcat()
    fdump.c:57: warning: initialization makes pointer from integer without a cast
    here is a snippet of my code that uses this function
    str is a char array
    and buffer is a buffer.

    insert
    Code:
    					for (i=0; i<bytesRead; i++) {
    						if (19<buffer[i]<127]{
    							printf["."];
    						}
    						else{	
    							printf("%s ", buffer[i]);
    						}
    						strcat(str,buffer[i]);
    						bytesPrinted++;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf["."];
    How does this even compile?

    And how did you declare str and buffer ?

    buffer[i] sounds like a single char, in which case there are several more mess-ups to deal with as well.
    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.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you include string.h? Otherwise, is "buffer" a character array? If so, you're doing it wrong. Also, your if check is wrong.


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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    printf["."] doesn't make any sense. strcat(str, buffer[i]) also doesn't make any sense, since you can only add a string to a string and buffer[i] is not a string but a single character. If you need to add a single character to str, then you probably want to do something like
    Code:
    str[some_appropriate_number] = buffer[i]
    and then make sure at the end that str is null-terminated.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Quote Originally Posted by tabstop View Post
    printf["."] doesn't make any sense. strcat(str, buffer[i]) also doesn't make any sense, since you can only add a string to a string and buffer[i] is not a string but a single character. If you need to add a single character to str, then you probably want to do something like
    Code:
    str[some_appropriate_number] = buffer[i]
    and then make sure at the end that str is null-terminated.
    Trust me it makes sense in my code.
    And thanks for the help!!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cloudsword View Post
    Trust me it makes sense in my code.
    And thanks for the help!!
    If you refer to printing the period, I can only hope that your code actually looks like
    Code:
    printf(".");
    instead of what you posted here.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    If you refer to printing the period, I can only hope that your code actually looks like
    I'm guessing they were referring to the buffer[ i ] statement.
    Code:
    char foo[BAR];
    char *baz[BAR];
    ...
    strcat( foo, baz[ i ] );
    It's possible they really did mean ... buffer[ i ].


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

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    I'm guessing they were referring to the buffer[ i ] statement.
    Code:
    char foo[BAR];
    char *baz[BAR];
    ...
    strcat( foo, baz[ i ] );
    It's possible they really did mean ... buffer[ i ].


    Quzah.
    Then I wish them the best of luck with finding a pointer between 19 and 127.

    Anyway given that the parenthesis in the if statement also mysteriously turned into a bracket I'm guessing it's someone who thinks re-typing the code and introducing strange new errors is cool and cutting-and-pasting the actual code is for losers.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    Then I wish them the best of luck with finding a pointer between 19 and 127.
    You mean like this?
    Code:
    char *buf[ 128 ];
    size_t x = 0;
    
    for( x = 0; x < 128; x++ )
    {
        buf[ x ] = malloc( 128 );
        sprintf( buf, "hello number %d!\n", x );
    }
    
    ...on to their code...
    
    strcat( foo, buf[ x ] );
    Something like that? But no, I don't know what they're really meaning to do there either.

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

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by tabstop View Post
    Then I wish them the best of luck with finding a pointer between 19 and 127.

    Anyway given that the parenthesis in the if statement also mysteriously turned into a bracket I'm guessing it's someone who thinks re-typing the code and introducing strange new errors is cool and cutting-and-pasting the actual code is for losers.
    It never ceases to amaze me how little pertinent information is included in some of these posts. I try to resist the temptation to make references to crystal balls or charades...it's difficult, though.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cloudsword View Post
    When I try to cocatenate a character in a buffer to my string, or character array, it gives me the warning. IT is the line where I am using strcat()
    There's your problem right there. You cant use strcat to concatenate a string and a character. You can use it to concatenate a null-terminated string of only one character onto another null-terminated string though. You need to make a null-terminated string of one character somewhere to use strcat.

    You'll get a warning on the printf line too where you're making the same mistake. However , unlike strcat, printf can be used to print a single character, but you need a different format specifier for that: "%c".

    Don't ask us to trust you, you don't have a clue what you're doing. Tis you who should trust us.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strcat
    By Countfog in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 03:16 PM
  2. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  3. strcat problems
    By cyberCLoWn in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2004, 04:52 PM
  4. strcat problems
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 06:51 AM
  5. Strcat function problems
    By s1k3 in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2001, 12:20 AM