473,804 Members | 3,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Concatenation Using Pointers No strcat()

1 New Member
Hi there,

I am having a bit of an issue when trying to concatenate strings using char * pointers.

Here is what I need:

I need to add a list of ID's to a string that is a SQL statement so that I can end up with:
SELECT DISTINCT Object_ID FROM Object_Interact ions WHERE Object_ID IN ('UUID1', 'UUID2') ORDER BY Object_ID ASC;

Here is what I am doing:
Expand|Select|Wrap|Line Numbers
  1. struct object_type
  2. {
  3.     char    *id;        /* UUID = 32 chars length */
  4. };
  5.  
  6. void strappnd(char *dest, char *src)
  7. {
  8.     /* appends one string to another */
  9.     while (*src != '\0')
  10.         *dest++ = *src++;
  11.  
  12.     *dest = '\0';
  13. }
  14.  
  15. char *buffer = NULL;
  16. char *sql = NULL;
  17. int j = 0;
  18. int buffer_size = 0;
  19. int object_count = 0;        
  20.  
  21. object_count = 2;        /* this is actually a calculated value... */
  22.  
  23. /* I have used malloc() and then assigned a value to object_list[j]->id and that was OK */
  24.  
  25. buffer_size = 32 * object_count + strlen("''") * object_count + strlen(", ") * (object_count - 1) + 1;
  26.  
  27. buffer = (char *)malloc(buffer_size * sizeof(char));    /* I know about pointer casting issue and malloc without stdlib.h but there is too much code to be changed... */
  28. strcpy(buffer, "");
  29.  
  30. for (j=0; j<object_count; j++)
  31. {
  32.     printf("buffer=%s\n", buffer);
  33.  
  34.     strcat(buffer, "'");    
  35.     strappnd(buffer, object_list[j]->id);
  36.  
  37.     /* we must not end the sequence with a comma */
  38.     if (j < object_count - 1)        
  39.         strcat(buffer, "', '");
  40.     else
  41.         strcat(buffer, "'");
  42. }
  43. sql = (char *)malloc((100 + buffer_size + 1) * sizeof(char));
  44. sprintf(sql, "SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (%s) ORDER BY Object_ID ASC;", buffer);
  45. free(buffer);    
  46.  
  47. printf("sql=%s\n", sql);
  48.  
Result:
sql = SELECT DISTINCT Object_ID FROM Object_Interact ions WHERE Object_ID IN (UUID1') ORDER BY Object_ID ASC;

I have been trying and searching for a couple of days for all kinds of solutions but I can't seem to find anything and I can't make it work.
Basically the question is how to transform a const char * into a char *. Does simple casting work? Is it safe? What else can I do because
I keep hitting this wall, and I can't use the standard <string.h> functions because most of them expect a char const * as opposed to
char * in one of the arguments.

I have been looking on the Internet and forums, but for most cases strcat is being used and seems to be enough.
I found interesting variations of strcat, but they don't seem to work and I don't have any more time for this. After finding libAPR I even
thought of using it, but I only need one function from this library and plus the application must be portable on Linux, Mac, Windows...

Any help on how to do string concatenation with char * or how to convert const char * to char * would be greatly appreciated.

Other than that, C is just great. :)

Thanks in advance,
Ovidiu Anghelidi
<e-mail address removed by weaknessforcats >
Dec 7 '07 #1
1 4431
weaknessforcats
9,208 Recognized Expert Moderator Expert
strappnd(buffer , object_list[j]->id);
My guess is the bug is here. Your strappnd() function does not append to the end of the string. It appends starting at the pointer address you give it, buffer in this case instead of the end fo the string in buffer.

Remove your strappnd() and replace it with strcat().

BTW: All of these C string functions in string.h have been deprecated by Microsoft and are not to be used in Windows code. There are safe versions of these but that will make the Windows version of your code not portable to Linux/Unix.
Dec 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

37
8550
by: Shri | last post by:
hi all, i am writing a code in which i have a char buffer "cwdir" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make my code Deamon. and later again i want to run some other executable available at the path holded by the "cwdir" using the system() system call. presently i concatenate program name (to be executed) to the "cwdir" and use system(chdir)to run the...
22
2091
by: jacob navia | last post by:
A function like strcpy takes now, two unbounded pointers. Unbounded pointers, i.e. pointers where there is no range information, have catastrophic failure modes specially when *writing* to main memory. A better string library would accept *bounded* pointers. We would have then: char *strcpyN(char *destination, size_t bound1, char *src,size_t bound2);
6
1698
by: Gary Morris | last post by:
Hi all, I tried posting this through a free news server, but it still has not appeared in Google, so if it turns up again I apologize. I hope someone can help me with this, or at least help me find some information that will help me. If I were not at my wit's end already, I wouldn't even ask. I'm used to doing all of my programming in Windows, but now I have a task to
23
3619
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
35
2471
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often see the use of sprintf() which results in several lines of code, and more processing than really...
5
4931
by: kaming | last post by:
Dear all, I would like to ask is these any DB2 function that can concatenation strings and have grouping capability?? for example, Tables:T F1 F2
87
5173
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
34
2670
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for string concatenation: x = "a" + "b"
8
1919
by: Sivakumar Kotamraju | last post by:
Hi, this is the code to print s. main() { sample s1(10,20,30); char *s; s="Hello"+s1; } friend char * operator +(char *s,sample &b) {
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.