?
Solved

Concatenating Strings

Posted on 2005-03-02
29
Medium Priority
?
315 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:mtcmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 5
  • +2
29 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13439579
>>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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13439675
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
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 252 total points
ID: 13439746

Conflict here:

      char dataLength[4];  

      strncat(dataLength, stringSize, 10);


You need to increase the size of dataLength[]



0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:mtcmedia
ID: 13439834
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
 
LVL 1

Expert Comment

by:amar_r
ID: 13439845
>>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
 
LVL 11

Expert Comment

by:griessh
ID: 13439869
>> 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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13439884
       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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13439891


Yeah.  As griessh poined out.  :)
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 13439967
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13439995

After:

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


put:

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


Kent
0
 
LVL 11

Expert Comment

by:griessh
ID: 13440077
       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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440086
No difference :(
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13440134
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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440136
griesh, that worked, whats going on?
0
 
LVL 11

Expert Comment

by:griessh
ID: 13440141
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
 
LVL 11

Assisted Solution

by:griessh
griessh earned 248 total points
ID: 13440169
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13440173

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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13440190
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13440208
Sorry ... for earlier pearlier read earlier ... though I like pearlier, it has a rather luminescent quality ;)

Paul
0
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440234
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
 
LVL 11

Expert Comment

by:griessh
ID: 13440239
>> 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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440317
char *data[6];

This holds 6 strings at the moment.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13440327
>>...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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13440564

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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440699
DATASIZE = strlen(data[0]);

Isnt a typo?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13440733

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
 
LVL 2

Author Comment

by:mtcmedia
ID: 13440750
datasize was global, no longer now so its no longer in caps.

data[] is an array of pointers to strings.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question