C 使用指针时检查字符串中的字符

C 使用指针时检查字符串中的字符,c,string,pointers,C,String,Pointers,我想检查字符串中的每个字符。字符串存储在指针中。 由于某种原因,我不能,它只让我得到整个字符串。 这是我的密码: int main() { char *s=(char*)calloc(30,sizeof(char)); s="hello"; printf("%s",&s[2]); return 0; } 这个代码打印“llo”,我只需要像“l”或“o”这样的1个字符。 有人知道我怎样才能做到吗? ty使用%c转换说明符打印sinlgechar字符,而不是使用%s打印字

我想检查字符串中的每个字符。字符串存储在指针中。 由于某种原因,我不能,它只让我得到整个字符串。 这是我的密码:

int main() {
  char *s=(char*)calloc(30,sizeof(char));
  s="hello";
  printf("%s",&s[2]);
  return 0;
 }
这个代码打印“llo”,我只需要像“l”或“o”这样的1个字符。 有人知道我怎样才能做到吗?
ty

使用
%c
转换说明符打印sinlge
char
字符,而不是使用
%s
打印字符串

另外,通过
calloc()
进行的内存分配是无用的,因为指向
char
s
的指针在一条语句之后由字符串literal
“hello”
的第一个元素的地址分配

#include <stdio.h>

int main (void) 
{
    const char *s = "hello";
    printf("%c", s[2]);
    return 0;
}
旁注:

  • 使用
    const
    限定符可防止对字符串文字的任何无意写入尝试导致错误

如果要分配内存并通过字符串literal分配/初始化分配的内存,请使用
strcpy()
(header
string.h
):

或者您可以使用
strdup()
(注意:
strdup()
不是标准C库的一部分),它将自动分配内存,并将字符串文本的字符串作为参数初始化传递:

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

int main (void) 
{
    char *s = strdup("hello");
    if (!s)            // Check if allocation failed.
    {
        fputs("Error at allocating memory!", stderr);
        exit(1);
    }

    printf("%c", s[2]);
    return 0;
}
#包括
#包括
内部主(空)
{
char*s=strdup(“你好”);
if(!s)//检查分配是否失败。
{
fputs(“分配内存时出错!”,stderr);
出口(1);
}
printf(“%c”,s[2]);
返回0;
}

旁注:

“字符串存储在指针中。”


这样的事情是不可能的。指针指向字符串的第一个元素(文本)。指针不存储字符串。

使用
%c
转换说明符打印sinlge
char
字符,而不是使用
%s
打印字符串

另外,通过
calloc()
进行的内存分配是无用的,因为指向
char
s
的指针在一条语句之后由字符串literal
“hello”
的第一个元素的地址分配

#include <stdio.h>

int main (void) 
{
    const char *s = "hello";
    printf("%c", s[2]);
    return 0;
}
旁注:

  • 使用
    const
    限定符可防止对字符串文字的任何无意写入尝试导致错误

如果要分配内存并通过字符串literal分配/初始化分配的内存,请使用
strcpy()
(header
string.h
):

或者您可以使用
strdup()
(注意:
strdup()
不是标准C库的一部分),它将自动分配内存,并将字符串文本的字符串作为参数初始化传递:

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

int main (void) 
{
    char *s = strdup("hello");
    if (!s)            // Check if allocation failed.
    {
        fputs("Error at allocating memory!", stderr);
        exit(1);
    }

    printf("%c", s[2]);
    return 0;
}
#包括
#包括
内部主(空)
{
char*s=strdup(“你好”);
if(!s)//检查分配是否失败。
{
fputs(“分配内存时出错!”,stderr);
出口(1);
}
printf(“%c”,s[2]);
返回0;
}

旁注:

“字符串存储在指针中。”


这样的事情是不可能的。指针指向字符串的第一个元素(文本)。指针不存储字符串。

Printf字符不是字符串。查找printf的格式代码,并询问自己,如果省略“&”,s[2]的值是多少。您还丢失了使用
calloc
创建的块(内存泄漏)。检查每个字符是什么意思?
printf(“%c”,s[2])改为尝试执行strcpy,将s设置为指向“hello”与复制字符串不同。请检查每个字符,我的意思是,如果我想查找字母“l”,或者检查所有字符串是仅数字字符串还是无数字字符串。Printf字符不是字符串。查找printf的格式代码,并询问自己,如果省略“&”,s[2]的值是多少。您还丢失了使用
calloc
创建的块(内存泄漏)。检查每个字符是什么意思?
printf(“%c”,s[2])改为尝试执行strcpy,将s设置为指向“hello”与复制字符串不一样。检查每个字符,我的意思是,如果我想找到字母“l”,或者检查所有字符串是仅数字还是无数字字符串。甚至strdup(“hello”)@pm100问题是,
strdup()
不是标准的一部分。我也不确定OP是否只为字符串literal
“hello”
分配内存。然后,他/她不会分配
30
char
元素的缓冲区,而只分配
6
char
元素的缓冲区。相当神秘的代码,很难。strdup是posixstandard@pm100把它纳入答案。谢谢以前也有点缺席。OP当然可以
realloc()。我也不确定OP是否只为字符串literal
“hello”
分配内存。然后,他/她不会分配
30
char
元素的缓冲区,而只分配
6
char
元素的缓冲区。相当神秘的代码,很难。strdup是posixstandard@pm100把它纳入答案。谢谢以前也有点缺席。如果需要,OP当然可以
realloc()
malloc。
#include <stdio.h>
#include <string.h>

int main (void) 
{
    char *s = strdup("hello");
    if (!s)            // Check if allocation failed.
    {
        fputs("Error at allocating memory!", stderr);
        exit(1);
    }

    printf("%c", s[2]);
    return 0;
}