Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / December 2006



Tip: Looking for answers? Try searching our database.

String concatenation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ivan - 18 Dec 2006 23:11 GMT
Hi I am programming in Xcode 2.3 a C application running Max OS X
10.4.7.  I cannot determine why the program wont compile when I add
char *temp line in the program.  I keep getting "invalid operands in
binary +" error for that line.  The program works like it should when
its just the printf line, but when i try to concatenate a temp string
so i can write it to a file, Xcode returns the error.

  char *temp =channel1[channelLocation]+"\t"+channel2[channelLocation]
    +"\t"+channel3[channelLocation]+"\t"+channel4[channelLocation];
  writeToFile(temp);
  printf("%d\t\t\t\t%d\t\t\t\t%d\t\t\t\t%d\n",
    channel1[channelLocation], channel2[channelLocation],
    channel3[channelLocation], channel4[channelLocation]);

I have tried other techniques like
 char temp[100]  -- same error

 char *temp = channel1[channelLocation];  -- compiles but program
crashes
 strcat(temp, "\t");
 strcat(temp, channel2[channelLocation]);
 ...

I google the error but i cant find a solid reason, let alone a fix.  So
if anybody can help i would greatly appreciate it.

Thanks,
Ivan
Sherm Pendley - 18 Dec 2006 23:32 GMT
> Hi I am programming in Xcode 2.3 a C application running Max OS X
> 10.4.7.  I cannot determine why the program wont compile when I add
[quoted text clipped - 5 lines]
>    char *temp =channel1[channelLocation]+"\t"+channel2[channelLocation]
>      +"\t"+channel3[channelLocation]+"\t"+channel4[channelLocation];

The compiler is complaining because it's a C compiler, and that's not C code.
C doesn't use + to concatenate strings. That's precisely what the quoted error
message is saying, that you're passing invalid operands (i.e. strings) to a
binary operator (+) that can't handle them.

>    writeToFile(temp);
>    printf("%d\t\t\t\t%d\t\t\t\t%d\t\t\t\t%d\n",
>      channel1[channelLocation], channel2[channelLocation],
>      channel3[channelLocation], channel4[channelLocation]);

If %d works to print these values, then they're not strings. They're ints.
So the above situation gets even worse - in addition to (mis)using + for
concatenation, you're also trying to concatenate strings with ints.

What you want is asprintf(), which is more or less the same as printf(),
except that it allocates a large-enough char* buffer and returns it, instead
of simply printing the results. Example:

   char *temp;
   asprintf(&temp, "%d\t%d\t%d\t%d\n", channel1[channelLocation],
                                       channel2[channelLocation],
                                       channel3[channelLocation],
                                       channel4[channelLocation]
           );
   if (NULL != temp) {
       // Buffer was allocated successfully
       writeToFile(temp);

       // You're responsible for free()ing temp when you're done with it
       free(temp);
   }

sherm--

Signature

Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Ivan - 18 Dec 2006 23:43 GMT
It works!!!  Thanks Sherm.  I dont think I would have figured that out
on my own.  Thanks again and for the fast response.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.