使用指针在函数中传递数组字符串时如何访问值? [英] How the values are accessed while passing an array string in a fucntion using a pointer?

查看:91
本文介绍了使用指针在函数中传递数组字符串时如何访问值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
#include < string.h >
void frequency_ptr( char * str);
int main()
{
char strings [ 30 ];
printf( 输入字符串:);
得到(字符串);
frequency_ptr(& strings [ 0 ]);
}
void frequency_ptr( char * strings)
{
int i = 0 ;
static int cnt [ 26 ];
while (* strings [i]!= ' \ 0'
{
if (* strings [i]> = ' a'&& * strings [i]< = ' z'
cnt [* strings [i] - ' 一个] ++;
i ++;
}
for (i = 0 ; i< 26; i ++)
printf( \ n%c出现%d次:' a' + i,cnt [i]);
}





我的尝试:



以下程序适用于我。但为什么我在上面的while循环中遇到错误。

  while (strings [i]!= '  \ 0'
{
if (strings [i ]> = ' a'&& strings [i]< = ' z'
cnt [strings [i] - ' a'] ++;
i ++;
}





提前致谢。

解决方案

代码我试过的是正确的。



理解指针和数组的关系是一个常见的C / C ++初学者问题。



但它被任何C / C ++书籍和教程所覆盖(在网上搜索阵列与指针之类的东西)。



例如,请参阅 C类 - 数组,字符串常量和指针 [ ^ ]。



在您的情况下,您有函数参数 char * strings 。这意味着字符串是指向 char 值的指针。使用解除引用运算符 - 维基百科 [ ^ ],您将访问该字符串的第一个字符:

  char  first_char = * strings; 

这也可以通过数组访问 [] 运算符来完成:

  char  first_char = strings [ 0 ]; 



所以使用

  char  c = *字符串[ 0 ]; 



  char  c = ** strings; 

并且两者都会导致编译错误,因为上述内容仅在字符串的类型为 char ** (指向 char 的指针)。


< blockquote>不要使用获取,而是使用 fgets 。尝试:

  #include   <   stdio.h  >  
void frequency_ptr( const char * str );

#define SIZE 30
#define LETTERS 26

int main()
{
char string [尺寸];
printf( 输入字符串:);
fgets(字符串, sizeof (字符串),stdin);
frequency_ptr(string);
return 0 ;
}

void frequency_ptr( const char * str)
{
unsigned int cnt [LETTERS] = { 0 };
while (* str!= ' \\ \\ 0'
{
if (* str> = ' a'&& * str< = ' z'
cnt [* str- ' a'] + +;
++ str;
}
size_t i;
for (i = 0 ; i< LETTERS; i ++)
printf( %c出现%d次。\ n,( char )(' a' + i),cnt [i]);
}


#include<stdio.h>
#include<string.h>
void frequency_ptr(char *str);
int main()
{
    char strings[30];
    printf("Enter a string:");
    gets(strings);
    frequency_ptr(&strings[0]);
}
void frequency_ptr(char *strings)
{
    int i=0;
    static int cnt[26];
    while(*strings[i]!='\0')
    {
    if(*strings[i]>='a' && *strings[i]<='z')
    cnt[*strings[i]-'a']++;   
    i++;
    }
    for(i=0;i<26;i++)
        printf("\n%c occurs %d times:", 'a'+i, cnt[i]);
}



What I have tried:

The below program works fine for me. But why am i getting error in the above while loop.

while(strings[i]!='\0')
 {
    if(strings[i]>='a' && strings[i]<='z')
    cnt[strings[i]-'a']++;
    i++;
 }



Thanks in advance.

解决方案

The code at "What I have tried" is correct.

It is a common C/C++ beginners problem to understand the relation of pointers and arrays.

But it is covered by any C/C++ books and tutorials (search the web for something like "array vs pointer").

See for example the section "Pointers and Arrays" at C Class - Arrays, String Constants and Pointers[^].

In your case you have the function parameter char *strings. That means strings is a pointer to char values. Using the Dereference operator - Wikipedia[^], you would access the first character of the string:

char first_char = *strings;

That can be also done with the array access [] operator:

char first_char = strings[0];


So using

char c = *strings[0];

is the same as

char c = **strings;

and both result in a compilation error because the above would be only valid when strings is of type char ** (pointer to pointer to char).


Don't ever use gets, use fgets instead. Try:

#include<stdio.h>
void frequency_ptr(const char *str);

#define SIZE 30
#define LETTERS 26

int main()
{
  char string[SIZE];
  printf("Enter a string:");
  fgets(string, sizeof(string), stdin);
  frequency_ptr(string);
  return 0;
}

void frequency_ptr(const char * str)
{
  unsigned int cnt[LETTERS]={0};
  while(*str != '\0')
  {
    if(*str>='a' && *str<='z')
      cnt[*str-'a']++;
    ++str;
  }
  size_t i;
  for(i=0; i<LETTERS;i++)
    printf("%c occurs %d times.\n", (char)('a'+i), cnt[i]);
}


这篇关于使用指针在函数中传递数组字符串时如何访问值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆