2

I am trying to insert value in to each element of array dynamically but couldn't make it work.

char itemCodeToSend[100]; 

So i used 'strcpy`

        char itemCode[100]  = "F,T,H";// So on
        char *  p    = strtok (itemCode, ","); // Parsing using comma
        int n_spaces = 0, i; 
        int position = 0;    

        while (p) { 
          strcpy(&itemCodeToSend[position],p);
          printf("<p>%i %s",position,&itemCodeToSend[position]);  
          position++;
          p = strtok (NULL, ",");
        }

      // Trying to print the array
        i=0;
      for (i; i < 34; i++){
          printf ("res[%d] = %s\n", i, &itemCodeToSend[i]);  
       }

While printing what i am expecting is

itemCodeToSend[0] = "F";
itemCodeToSend[1] = "T";
itemCodeToSend[2] = "H"; 

But What i am getting is

itemCodeToSend[0] = "FTH";
itemCodeToSend[1] = "TH";
itemCodeToSend[2] = "H";   

Not sure how to handle this.

21
  • can you paste code that compiles? Commented May 7, 2014 at 18:05
  • If you are "new to C" and don't know C++, don't cross tag. The modern languages are very different from each other. Commented May 7, 2014 at 18:06
  • 1
    Can you post the command where you print the output? Commented May 7, 2014 at 18:07
  • 1
    char[100] test; Am I missing something? Commented May 7, 2014 at 18:10
  • 1
    Really, you need to see the print? He's using strcpy to copy an int to a char array, the output is the least of the problems ;) Commented May 7, 2014 at 18:11

3 Answers 3

3

If I correctly understood your problem, then it is pretty straight forward. If you just have to parse characters with comma delimited, then a simple while loop would suffice. No need to make things complicated by using heavy functions where it is not needed.

   #include <stdio.h>
    int main(){

        char itemCode[100]  = "F,T,H";  // So on
        int i = 0,j=0;
        while (itemCode[i] != '\0') { 
            if(itemCode[i] == ',');
            else{
                printf("itemCodeToSend[%d] = \"%c\"\n",j,itemCode[i]);
                j++;
            }
            i++;
        }
        return 0;
    }

It outputs:

itemCodeToSend[0] = "F";
itemCodeToSend[1] = "T";
itemCodeToSend[2] = "H"; 

I know you should not just want to print the parsed character but whatever you want to do, you can always place your logic inside the while loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Utkal. Yes its simple and nice too..+1. Actually i tried doing something like your's but got error while comparing two strings. I know what i done wrong i used strcmp to compare char[i] and constant string. That's why i gone in this route and posted my answer as well.
1

It seems you are confusing characters with strings. In C:

  • char is the byte type. It can hold a single character (actually depends on the encoding).
  • char[N] is an array of N bytes. It can be seen as a string of up to N-1 characters, NUL terminated. It will decay on most uses to a char* that can be seen as a NUL-terminated string of an unspecified maximum length.

If you want an array of 100 strings up to 19 characters each, you can write:

char arrayOfStrings[100][20];

The rest of your code seems right to me.

Comments

0

Finally made it work using the replacement code below. I think the problem is the strcpy. I got the expected output by replacing

strcpy(&itemCodeToSend[position],p);

with the following code

itemCodeToSend[position] = strtol(p, 0, 16); 

Hope it will help someone like me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.