Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

String Concatenation Using Pointers No strcat()Helpful Member! 

Raidwan (TechnicalUser) (OP)
7 Dec 07 10:32
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_Interactions WHERE Object_ID IN ('UUID1', 'UUID2') ORDER BY Object_ID ASC;

Here is what I am doing:

struct object_type
{
char *id; /* UUID = 32 chars length */
};

void strappnd(char *dest, char *src)
{
/* appends one string to another */
while (*src != '\0')
*dest++ = *src++;

*dest = '\0';
}

char *buffer = NULL;
char *sql = NULL;
int j = 0;
int buffer_size = 0;
int object_count = 0;

object_count = 2; /* this is actually a calculated value... */

/* I have used malloc() and then assigned a value to object_list[j]->id and that was OK */

buffer_size = 32 * object_count + strlen("''") * object_count + strlen(", ") * (object_count - 1) + 1;

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... */
strcpy(buffer, "");

for (j=0; j<object_count; j++)
{
printf("buffer=%s\n", buffer);

strcat(buffer, "'");
strappnd(buffer, object_list[j]->id);

/* we must not end the sequence with a comma */
if (j < object_count - 1)
strcat(buffer, "', '");
else
strcat(buffer, "'");
}
sql = (char *)malloc((100 + buffer_size + 1) * sizeof(char));
sprintf(sql, "SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (%s) ORDER BY Object_ID ASC;", buffer);
free(buffer);

printf("sql=%s\n", sql);

Result:
sql = SELECT DISTINCT Object_ID FROM Object_Interactions 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 very much.
Ovidiu Anghelidi

ovidiu@intelligencerealm.com

Helpful Member!  cpjust (Programmer)
7 Dec 07 11:21
Passing a char* to a function that expects a const char* is no problem since the const-ness isn't being lowered, it's being increased.
Passing a const char* to a function expecting a char* is where you'd run into trouble.  If you have a const char*, that implies that the value of the string should not be modified; but since the function takes a char* parameter, that implies that the function wants to modify it.
fpmurphy (TechnicalUser)
8 Dec 07 8:54
You have a fundametnal problem in your code. You are overwriting your buffer each time to attempt to attempt an object_id.

CODE

   strappnd(buffer, object_list[j]->id);
            ^^^^^^

You need to append to the end of the buffer.

Here is a sample program which should help you understand what you need to do:

CODE

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

struct object_type
{
    char *id;            /* UUID = 32 chars length */
} object_list[2];

static char UUID1[32] = "1111111111111111111111111111111";
static char UUID2[32] = "2222222222222222222222222222222";

int
main( int argc, char *argv[])
{
    char *buffer = NULL;
    char *sql = NULL;
    int j = 0;
    int buffer_size = 0;
    int object_count = 2;
    char *bufptr;
    char *t;

    /* hack to populate arrary of structures */
    object_list[0].id = (char *) UUID1;
    object_list[1].id = (char *) UUID2;

    buffer_size = 32 * object_count + strlen("''") *              object_count + strlen(", ") * (object_count - 1) + 1;
    buffer = (char *)malloc(buffer_size * sizeof(char));
    bzero(buffer, sizeof(buffer));
    bufptr = buffer;

    for (j=0; j < object_count; j++)
    {
        t = (char *)object_list[j].id;

        if (j)
           *bufptr++ = ',';
        *bufptr++ = '\'';
        while (*t)
            *bufptr++ = *t++;
        *bufptr++ = '\'';
        *bufptr = '\0';

        printf ("BUFFER: %s\n", buffer);
    }

    sql = (char *)malloc((100 + buffer_size + 1) * sizeof(char));

    sprintf(sql, "SELECT DISTINCT Object_ID FROM Object_Interactions WHERE Object_ID IN (%s) ORDER BY Object_ID ASC;", buffer);
    free(buffer);

    printf("sql=%s\n", sql);
}

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close