views:

98

answers:

3
char *funcNames[]= {"VString","VChar","VArray","VData"};

    for(int i=0;i<4;i++)
    {

     char* temp = funcNames[i];
     int len = strlen(funcNames[i]);
     for(int j = 0;j<len ;j++)
     {
      if(j!=0)
      {
       char arr = temp[j];
      }
     }
}

here i want to separate "V" from all string in char array ...and create another char array without "V" in starting of the string.i want another char array {String,char,array,data}...i cant make a char array ....help me to solve my issue...

+5  A: 

Do you really need a copy? You could just make a new array pointing into the original strings:

char *newArray[4];
for (i = 0; i < 4; i++) {
  newArray[i] = funcNames[i] + 1;
}
Carl Norum
never exactly copy carl,i want skip the first letter of the string ...
Rajakumar
Then this solution is probably better for you, why waste time and memory copying strings when the original ones will do just fine?
Carl Norum
Carl's code is correct - it does skip the first letter of each string.
Keith Randall
yes .....you are correct ......i can get desired output thank you carl Norum.....
Rajakumar
after the thank you, your supposed to tick answer as accepted?
Madi D.
+2  A: 

There's only small differences between arrays and pointers so I'd opt for:

#include <stdio.h>
#include <string.h>
#include <assert.h>

int main (void) {
    int i;
    char *funcNames[]= {"VString","VChar","VArray","VData"};

    // This is the code that dupicates your strings by allocating an array,
    //   then allocating each string within that array (and copying).
    // Note we use strlen, not strlen+1 to mallocsince we're replacing the
    //  'V' at the start with the zero byte at the end. Also we strcpy
    // from char offset 1, not 0 (to skip the fist char).

    char **newNames = malloc (sizeof(char*) * sizeof(funcNames) / sizeof(*funcNames));
    assert (newNames != NULL);
    for (i = 0; i < sizeof(funcNames) / sizeof(*funcNames); i++) {
        newNames[i] = malloc (strlen (funcNames[i]));
        assert (newNames[i] != NULL);
        strcpy (newNames[i], funcNames[i] + 1);
    }

    /* Use your newNames here */

    for (i = 0; i < sizeof(funcNames) / sizeof(*funcNames); i++) {
        printf ("funcNames[%d] @%08x = '%s'\n", i, funcNames[i], funcNames[i]);
        printf (" newNames[%d] @%08x = '%s'\n", i,  newNames[i],  newNames[i]);
        putchar ('\n');
    }

    // Finished using them.

    // Free the strings themselves, then free the array.

    for (i = 0; i < sizeof(funcNames) / sizeof(*funcNames); i++)
        free (newNames[i]);
    free (newNames);

    return 0;
}

You can see from the output that the locations of the variables in memory are different and that the content of the new strings is what you wanted:

funcNames[0] @00402000 = 'VString'
 newNames[0] @006601c0 = 'String'

funcNames[1] @00402008 = 'VChar'
 newNames[1] @006601d0 = 'Char'

funcNames[2] @0040200e = 'VArray'
 newNames[2] @006601e0 = 'Array'

funcNames[3] @00402015 = 'VData'
 newNames[3] @006601f0 = 'Data'
paxdiablo
I guess you mean `assert (newNames[i] != NULL);`
pierr
Yes, I use it so rarely I forgot the sense was reversed :-) Fixed in update.
paxdiablo
+2  A: 

If you do need to make copies then you'll have to use dynamical allocation to create the buffers to hold the copies. What you will do is declare an array of pointers and place an allocated string buffer in each of the array's entries:

char *newArray[4];
for (i = 0; i < 4; i++) {
   newArray[i] = malloc(sizeof(char) * streln(funcNames[0]));
   strcpy(newArray[i], funcNames[i] + 1);
 }

You will have to call free() on each allocated buffer.

Or if you don't want to do allocation and are know the maximum length of the strings in funcNames:

#define MAX_FUNC_NAME_LEN 32
char newArray[4][MAX_FUNC_NAME_LEN];
for (i = 0; i < 4; i++) {
   strcpy(newArray[i], funcNames[i] + 1);
 }
shf301