C语言 如何从C字符串中删除第一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4295754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:13:55  来源:igfitidea点击:

How to remove first character from C-string?

cstringcstring

提问by Ash

Can anyone please help me? I need to remove the first character from a char *in C.

谁能帮帮我吗?我需要从char *C 中的a中删除第一个字符。

For example, char * contentscontains a '\n'character as the first character in the array. I need to detect and eliminate this character, modifying the original variable after its been "sanitized".

例如, char * contents包含一个'\n'字符作为数组中的第一个字符。我需要检测并消除这个字符,在“清理”后修改原始变量。

Can anyone help me with the code? I'm completely new to C, and just can't seem to figure it out.

任何人都可以帮助我的代码?我对 C 完全陌生,似乎无法弄清楚。

回答by ruslik

if (contents[0] == '\n') 
    memmove(contents, contents+1, strlen(contents));

Or, if the pointer can be modified:

或者,如果可以修改指针:

if (contents[0] == '\n') contents++;

回答by ruslik

char* contents_chopped = contents + 1;

char* contents_chopped = contents + 1;

This will result in contents_choppedpointing to the same string, except the first char will be the next after \n

这将导致contents_chopped指向相同的字符串,除了第一个字符将是 \n 之后的下一个字符

Also, this method is faster.

此外,这种方法速度更快。

回答by mfisch

Do not just increment the pointer if you have malloc'd any memory or your program will crash.free needs the original pointer. You can copy the pointer, make a new chunk of memory and memcpy it, access it as ptr+1 or any of a bunch of other ways, but the people who say just increment the pointer are giving you dangerous advice. You can run this sample program and see what happens when you "just increment the pointer".

如果您已经分配了任何内存,请不要只增加指针,否则您的程序将崩溃。free 需要原始指针。您可以复制指针,创建一个新的内存块并对其进行 memcpy,以 ptr+1 或任何其他方式访问它,但是那些说只增加指针的人给了您危险的建议。您可以运行这个示例程序,看看当您“只增加指针”时会发生什么。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
    char *str = (char *)malloc(10);
    strcpy(str, "1234567890");
    printf("%s\n", str);
    str++;
    printf("%s\n", str);
    free(str);
}

Hint: Here's the result:

提示:结果如下:

[mfisch@toaster ~]$ ./foo
1234567890
234567890
*** glibc detected *** ./foo: free(): invalid pointer: 0x08c65009 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x724591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x725de8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x728ecd]
./foo[0x80484e3]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x6cfbd6]
./foo[0x80483f1]
======= Memory map: ========
001c9000-001e4000 r-xp 00000000 08:01 2883609    /lib/ld-2.11.1.so
001e4000-001e5000 r--p 0001a000 08:01 2883609    /lib/ld-2.11.1.so
001e5000-001e6000 rw-p 0001b000 08:01 2883609    /lib/ld-2.11.1.so
006b9000-0080c000 r-xp 00000000 08:01 3015690    /lib/tls/i686/cmov/libc-2.11.1.so
0080c000-0080d000 ---p 00153000 08:01 3015690    /lib/tls/i686/cmov/libc-2.11.1.so
0080d000-0080f000 r--p 00153000 08:01 3015690    /lib/tls/i686/cmov/libc-2.11.1.so
0080f000-00810000 rw-p 00155000 08:01 3015690    /lib/tls/i686/cmov/libc-2.11.1.so
00810000-00813000 rw-p 00000000 00:00 0
00e4d000-00e4e000 r-xp 00000000 00:00 0          [vdso]
00fe0000-00ffd000 r-xp 00000000 08:01 2883667    /lib/libgcc_s.so.1
00ffd000-00ffe000 r--p 0001c000 08:01 2883667    /lib/libgcc_s.so.1
00ffe000-00fff000 rw-p 0001d000 08:01 2883667    /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:01 9700477    /home/mfisch/foo
08049000-0804a000 r--p 00000000 08:01 9700477    /home/mfisch/foo
0804a000-0804b000 rw-p 00001000 08:01 9700477    /home/mfisch/foo
08c65000-08c86000 rw-p 00000000 00:00 0          [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b776f000-b7770000 rw-p 00000000 00:00 0
b7780000-b7783000 rw-p 00000000 00:00 0
bfc22000-bfc37000 rw-p 00000000 00:00 0          [stack]
Aborted

回答by Karl Knechtel

It sounds as if you're under the impression that a char* "contains" characters. It does not. It merely pointsat abyte. The rest of the string is implied to consist of the subsequent byte in memory up until the next null byte. (You should also be aware that although the 'char' data type is a byte, by definition, it is not really a character - please be aware of Unicode - and nor is a byte necessarily an octet.)

听起来好像你的印象是 char* “包含”字符。它不是。它只是指出,在一个字节。字符串的其余部分暗示由内存中的后续字节组成,直到下一个空字节。(您还应该知道,尽管 'char' 数据类型是一个字节,但根据定义,它并不是真正的字符 - 请注意 Unicode - 字节也不一定是八位字节。)

The char* is not an array, either, although there may exist an array of characters such that the pointer is pointing to the beginning of that array.

char* 也不是数组,尽管可能存在一个字符数组,使得指针指向该数组的开头。

回答by deepak kumar Pranjay

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

int main ()
 {
char src[50] = "123456789123434567678";

char dest[16]={0};
 memcpy(dest, src+1,sizeof(src));
 printf("%s\n",dest);
 return(0);
}

src+1 -> indicate how many char you want to remove

回答by Sam

Here is my code

这是我的代码

char  * bastakiniSil(char *contents){
char *p = malloc( sizeof(*p) * strlen(contents) );
int i;
for(i=0; i<strlen(contents); i++)
{
    p[i]=contents[i+1];
}

return p;

}

}