1

I have a char array LL,4014.84954 that I send into a function like this example:

#include <stdio.h>
#include <math.h>

void myFunction(char* in_string, char* out_string) {
  printf("Start_String=%s\n", in_string);
  int in_size = (int)(sizeof(in_string));
  printf("%d\n", in_size);
  int i = 0;
  for(i = 0; i <= in_size-ceil(in_size/2); i++) {
    out_string[i] = in_string[i];
  }
}

int main(int arg) {
  char in_string[] = "LL,4014.84954";
  char out_string[] = "";
  printf("In_String=%s\n", in_string);
  myFunction(in_string, out_string);
  printf("Out_String=%s\n", out_string);
}

My question has two parts.

  1. How do I get the length of this char array? int in_size = (int)(sizeof(in_string)); in this example gives me 8 which is the size of the pointer (long int). I know I could make a for loop that marches through until it see the null termination, but is there a nicer way? I previously was using char[] and sizeof works great, but now I am converting to char*.

  2. How can I write a portion of these chars to out_string. My example currently writes all chars to out_string.

Here is the raw output:

In_String=LL,4014.84954
Start_String=LL,4014.84954
8
Out_String=LL,40014.84954
Kyle Weller
  • 2,533
  • 9
  • 35
  • 45
  • Using the `sizeof()` a *pointer* probably isn't helping your cause. You may want to review [this question/answer](http://stackoverflow.com/questions/720077/calculating-size-of-an-array) along with the answers below. – WhozCraig Mar 23 '13 at 19:21
  • What portion of the chars? If it's just from a set offset to the end, it's very simple. – teppic Mar 23 '13 at 19:29

2 Answers2

1

Regarding your first question, use strlen().

Regarding the second question, first of all you need to make sure that out_string is wide enough to accommodate the result. Currently, it isn't, and the behaviour of your code is undefined. Once you fix that, to copy a portion of the string you'd need to change the initial and final conditions of the for loop, not forgetting about the NUL terminator.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • And `strlen` is a well debugged implementation of walking along the string until you encounter a `\0`. That's one of the costs of using ` terminated` strings instead of `(length, buffer)` strings as for instance pascal does. – dmckee --- ex-moderator kitten Mar 23 '13 at 19:21
1

(1) Answer to question 2:

char out_string[] = "";

out_string[] is of only one size. you assigning out_string[i] = ... for i > 0 is wrong and cause an undefined error. Instead of this you should declare out_string[] like this:

out_string[] = malloc(strlen(in_string) + 1);

//                                        ^ extra for \0 char

(2) additionally @WhozCraig commenting correct, you actually need strlen() to find length of string. sizeof you are using wrong.

So replace:

int in_size = (int)(sizeof(in_string));

by

int in_size = strlen(in_string);

(3) Also, what is in_size-ceil. As I can understands from your raw output you don't need such kind of function and calculations. Just replace your for loop:

for(i = 0; i <= in_size-ceil(in_size/2); i++) 

by

for(i = 0; i < in_size; i++) 

(4) At the end don;t forget to terminate you string out_string, after for loop add this line

out_string[i] = '\0'
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208