• C

Concatenating Strings

Hello Experts,

I am trying to concatenate Strings, and am having problems, my program just freezes, with no errors etc.

        if (DATASIZE < 10) { /* if less than 10, need to add a zero */
            printf("less than 10\n");
            strncat(dataLength, "0", 1);
            strncat(dataLength, stringSize, 10);
        } else {
        printf("more than 10\n");
            strncat(dataLength, stringSize, 10);
        }
       
        printf("outside the if else");

Here my code will either print "less than 10" or "more than 10" but never "outside the if else". stringSize, when printed contains the correct value and is defined as: char stringSize[50]. dataLength is defined as char *dataLength and starts off with nothing in it.
LVL 2
mtcmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
>>dataLength is defined as char *dataLength
This is almost certainly the problem. You have to initialise a pointer to point to some memory before copying to it, otherwise you will be writing to random locations and crashing. Post more of your code and we'll take a closer look.

I'd guess that you have something like:

char * datalength;
if (....)
...

Change this to something like:

char * dataLength = malloc ( 100 );
...

or char dataLength[100];

You seem quite confused about variabkle naming. I'd suggest, again, that you post more/all of your code and we'll check it over.

Paul
0
mtcmediaAuthor Commented:
I have updated the code:

        char *blockedData;
        char dataLength[4];      
        char stringSize[50];

        DATASIZE = strlen(data[0]);
        printf("DATASIZE: %d\n", DATASIZE);
        sprintf(stringSize, "%i", DATASIZE); /* convert it to a String */                
        printf("stringSize Is %s\n", stringSize);
       
        if (DATASIZE < 10) { /* if less than 10, need to add a zero */
            printf("less than 10\n");
            strncat(dataLength, "0", 1);
            printf("less than 10\n");
            strncat(dataLength, stringSize, 10);
            printf("less than 10\n");
        } else {
        printf("more than 10\n");
            strncat(dataLength, stringSize, 10);
        }
       
        printf("outside the if else");

If DATASIZE is less than 10, it prints out the "less than 10" all 3 times, but never prints "outside the if else"
0
Kent OlsenData Warehouse Architect / DBACommented:

Conflict here:

      char dataLength[4];  

      strncat(dataLength, stringSize, 10);


You need to increase the size of dataLength[]



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

mtcmediaAuthor Commented:
Well spotted, however still the same issue. I updated too:

 if (DATASIZE < 10) { /* if less than 10, add a zero */
            printf("less than 10\n");
            strncat(dataLength, "0", 1);
            printf("less than 10\n");
            strncat(dataLength, stringSize, 4);
            printf("less than 10\n");
        } else {
        printf("more than 10\n");
            strncat(dataLength, stringSize, 4);
        }

As stringSize will never be larger than 4.
0
amar_rCommented:
>>dataLength is defined as char *dataLength

  As paulcaswell said,problem with your char *datalength;

  Change to array of characters  as      char dataLength[100];
 
 
   It can solve your problem.

0
griesshCommented:
>> strncat(dataLength, "0", 1);

dataLength is not used yet, appending won't work! Use sprintf() instead.

>> strncat(dataLength, stringSize, 10)
As already pointed out, size of dataLength is not big enough.

Why don't you do it much easier:
sprintf(stringSize, "%i", DATASIZE)
if (DATASIZE < 10)
{
  sprintf (dataLength, "0%i", DATASIZE);
}
else
{
  sprintf (dataLength, "%i", DATASIZE);
}
0
Kent OlsenData Warehouse Architect / DBACommented:
       char *blockedData;
        char dataLength[4];      
        char stringSize[50];

        DATASIZE = strlen(data[0]);


data[] contains whatever was left on the stack by a previous process.  You need to initialize it.

Kent
0
Kent OlsenData Warehouse Architect / DBACommented:


Yeah.  As griessh poined out.  :)
0
mtcmediaAuthor Commented:
Ah thats a nice way of doing it, thanks greissh!

However, still same problem, it wont reach the next println:

        DATASIZE = strlen(data[0]); /* get the length of the data itself (+ 1 )? */
        printf("DATASIZE: %d\n", DATASIZE);
        sprintf(stringSize, "%i", DATASIZE); /* convert it to a String */                
        printf("stringSize Is %s\n", stringSize);
       
        sprintf(stringSize, "%i", DATASIZE);
       
        if (DATASIZE < 10) {
            sprintf (dataLength, "0%i", DATASIZE);
        } else {
            sprintf (dataLength, "%i", DATASIZE);
        }
       
        printf("outside the if else");

Output is:

DATASIZE: 4
stringSize Is 4
0
Kent OlsenData Warehouse Architect / DBACommented:

After:

       char *blockedData;
        char dataLength[4];      
        char stringSize[50];


put:

   dataLength[0] = 0;
   stringSize[0] = 0;


Kent
0
griesshCommented:
       DATASIZE = strlen(data[0]); /* get the length of the data itself (+ 1 )? */

        if (DATASIZE < 10) {
            sprintf (dataLength, "0%i", DATASIZE);
        } else {
            sprintf (dataLength, "%i", DATASIZE);
        }
       
        printf("outside the if else, data size = %s\n", dataLength);
0
mtcmediaAuthor Commented:
No difference :(
0
PaulCaswellCommented:
Could this be what you wanted all along?

char dataLength[4]; // 4 = two characters plus one terminator + one for luck.
sprintf (dataLength, "%02.2d", strlen(data[0]));

Paul
0
mtcmediaAuthor Commented:
griesh, that worked, whats going on?
0
griesshCommented:
A program like this

#include <stdio.h>

void main()
{
       char dataLength[4];
       int DATASIZE = 4;

        if (DATASIZE < 10) {
            sprintf (dataLength, "0%i", DATASIZE);
        } else {
            sprintf (dataLength, "%i", DATASIZE);
        }
       
        printf("outside the if else, data size = %s\n", dataLength);

}

should run without any problems. Are you doing anything after the printf?
0
griesshCommented:
output operations are cached on many systems and don't take place right away unless you flush the buffer. You didn't end the printf() with  a \n so I guess that printf was cached.
0
Kent OlsenData Warehouse Architect / DBACommented:

Hi Paul,

char dataLength[4]; // 4 = two characters plus one terminator + one for luck.
sprintf (dataLength, "%02.2d", strlen(data[0]));


Typo with strlen(data[0])???

0
PaulCaswellCommented:
Kent,

Maybe not! Take a look at askers pearlier post:
>>        DATASIZE = strlen(data[0]); /* get the length of the data itself (+ 1 )? */

I'm guessing that data is a char ** perhaps.

Paul
0
PaulCaswellCommented:
Sorry ... for earlier pearlier read earlier ... though I like pearlier, it has a rather luminescent quality ;)

Paul
0
mtcmediaAuthor Commented:
Griesh, I see now :) My program had an error under the println also, i didnt realise this would have an effect.

Now, how do I split the points on this, all you guys are great!
0
griesshCommented:
>> char **

I was wondering that myself, but the asker is a beginner (I guess) so it would surprise me if they knew how to deal with that.
Independant of that, the length is a vaild integer and that's all of the code we have.
0
mtcmediaAuthor Commented:
char *data[6];

This holds 6 strings at the moment.
0
PaulCaswellCommented:
>>...is a beginner (I guess)...
Indeed but replace data[0] with argv[0] and you've got a very common code fragment.

Anyhoo, about points splitting, there's usually a 'Split Points' button somewhere, I can never remember where.

I'd suggest most to griessh as he spotted the actual bug first (although I was on the way there myself).

None for Kent cause he's got too many points as it is ... just kidding kent ;)

Paul
0
Kent OlsenData Warehouse Architect / DBACommented:

Hey -- As I'm so fond of saying, I don't care who gets the points, as long as we keep Sunny out of it.  :)

I know, give 'em to Ryan Stiles.  I don't think that he's ever won.  ;)



BTW, with the code snippets we'd seen, I assume that

>>        DATASIZE = strlen(data[0]); /* get the length of the data itself (+ 1 )? */

was also a typo and the warning level was set too low.

0
mtcmediaAuthor Commented:
DATASIZE = strlen(data[0]);

Isnt a typo?
0
Kent OlsenData Warehouse Architect / DBACommented:

Just curious, but why is DATASIZE all caps?

Your stack variables are mixed case.

data is all lower case.  (I assume that it's global.)


Kent
0
mtcmediaAuthor Commented:
datasize was global, no longer now so its no longer in caps.

data[] is an array of pointers to strings.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.