: 당신은 출력에서 볼 수
#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;
}
그 메모리에있는 변수의 위치는 다른 새로운 문자열의 내용 것을 당신이 원하는 무엇인가 :
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'
결코 정확히 내가 문자열의 첫 글자를 건너 뛰고 싶어 칼을 복사 ... – Rajakumar
을 그렇다면이 솔루션은 아마도 여러분에게 더 좋을 것입니다. 왜 원래의 것들이 잘 될 때 문자열을 복사하고 낭비하는 시간을 낭비할까요? –
Carl의 코드가 정확합니다. 각 문자열의 첫 번째 문자는 건너 뜁니다. –