Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用结构的同时将字符串从数组传递到指针(在c中)_C_Arrays_String_Pointers_Structure - Fatal编程技术网

如何在使用结构的同时将字符串从数组传递到指针(在c中)

如何在使用结构的同时将字符串从数组传递到指针(在c中),c,arrays,string,pointers,structure,C,Arrays,String,Pointers,Structure,此代码的要点是从用户处读取一个字符串(少于50个字符),然后使用letters函数将字符串中的字母放入指针中,这样每个字母只运行一次,然后还计算每个字母的出现次数。最后,通过使用报告功能,它应该在屏幕上输出我刚才解释的所有内容。例如 用户输入“Hello”,程序输出: H : 1 e : 1 l : 2 o : 1 守则: #include <stdio.h> #include <stdlib.h> #include <string.

此代码的要点是从用户处读取一个字符串(少于50个字符),然后使用letters函数将字符串中的字母放入指针中,这样每个字母只运行一次,然后还计算每个字母的出现次数。最后,通过使用报告功能,它应该在屏幕上输出我刚才解释的所有内容。例如 用户输入“Hello”,程序输出:

H : 1    
e : 1    
l : 2   
o : 1    
守则:

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

struct charact {
    char ch;
    int occurs;
    struct charact *next;
};

typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars);
Char * createnode(char ch);

int main() {
    char name[50];
    ListofChar chars = NULL;
    scanf("%s", name);
    letters(name, &chars);
    report(chars);
    return 0;
}

Char * createnode(char ch) {
    CharNode_ptr newnode_ptr ;
    newnode_ptr = malloc(sizeof (Char));
    newnode_ptr -> ch = ch;
    newnode_ptr -> occurs = 0;
    newnode_ptr -> next = NULL;
    return newnode_ptr;
}

void letters(char name[50], ListofChar * lst_ptr) {
    int i;
    for(i=0; name[i]!='\0'; i++){
        //everything is done here
    }
    return;
}

void report(ListofChar chars) {
    int i;
    // this is only to output the results
    return;
}
#包括
#包括
#包括
结构特征{
char ch;
int出现;
结构特征*下一步;
};
typedef结构字符字符;
typedef Char*ListofChar;
typedef Char*CharNode_ptr;
无效字母(字符名称[50],ListofChar*chars_ptr);
作废报告(现金清单);
Char*createnode(Char-ch);
int main(){
字符名[50];
ListofChars=NULL;
scanf(“%s”,名称);
字母(姓名和字符);
报告(chars);
返回0;
}
Char*createnode(Char ch){
CharNode_ptr newnode_ptr;
newnode_ptr=malloc(sizeof(Char));
新建节点\u ptr->ch=ch;
newnode_ptr->occurs=0;
newnode_ptr->next=NULL;
返回newnode\u ptr;
}
无效字母(字符名称[50],ListofChar*lst_ptr){
int i;
对于(i=0;名称[i]!='\0';i++){
//一切都在这里完成了
}
回来
}
作废报告(现金清单){
int i;
//这只是为了输出结果
回来
}

提前感谢

根据我的理解,您希望使用单链表将节点连接在一起。在示例程序中,我在开始时添加了新节点。头指针保存最后添加的节点的地址

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

struct charact {
 char ch;
 int occurs;
 struct charact *next;
};

typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars);
Char * createnode(char ch, ListofChar * head_ptr);

int main() {
 char name[50];
 ListofChar chars = NULL;
 scanf("%s", name);
 letters(name, &chars);
 report(chars);
 return 0;
}

Char * createnode(char ch,ListofChar * head_ptr ) {
  CharNode_ptr newnode_ptr;
  ListofChar  temp;
  temp = *head_ptr;
  while(temp) {
     if( temp->ch  == ch) {
     temp->occurs += 1;
     return;
     }
     temp= temp->next;
 }
 newnode_ptr = malloc(sizeof (Char));
 newnode_ptr -> ch = ch;
 newnode_ptr -> occurs = 1;
 newnode_ptr -> next = *head_ptr;
  *head_ptr = newnode_ptr;
  return newnode_ptr;
 }

void letters(char name[50], ListofChar * lst_ptr) {
  int i;
  for(i=0; name[i]!='\0'; i++){
     createnode(name[i],lst_ptr);
     //everything is done here
   }
 return;
  }

 void report(ListofChar chars) {
    int i;
    while(chars)
     {
   printf("%c %d \n",chars->ch,chars->occurs);
   chars = chars->next;
    }
   //printf("%c \n",chars->ch);
   // this is only to output the results
   return;
   }
#包括
#包括
#包括
结构特征{
char ch;
int出现;
结构特征*下一步;
};
typedef结构字符字符;
typedef Char*ListofChar;
typedef Char*CharNode_ptr;
无效字母(字符名称[50],ListofChar*chars_ptr);
作废报告(现金清单);
Char*createnode(Char ch,ListofChar*head_ptr);
int main(){
字符名[50];
ListofChars=NULL;
scanf(“%s”,名称);
字母(姓名和字符);
报告(chars);
返回0;
}
Char*createnode(Char ch,ListofChar*head_ptr){
CharNode_ptr newnode_ptr;
ListofChar-temp;
温度=*压头温度;
while(临时){
如果(温度->通道==通道){
温度->发生+=1;
回来
}
温度=温度->下一步;
}
newnode_ptr=malloc(sizeof(Char));
新建节点\u ptr->ch=ch;
newnode_ptr->occurs=1;
newnode_ptr->next=*head_ptr;
*head_ptr=newnode_ptr;
返回newnode\u ptr;
}
无效字母(字符名称[50],ListofChar*lst_ptr){
int i;
对于(i=0;名称[i]!='\0';i++){
createnode(名称[i],lst_ptr);
//一切都在这里完成了
}
回来
}
作废报告(现金清单){
int i;
while(chars)
{
printf(“%c%d\n”,字符->字符,字符->出现);
chars=chars->next;
}
//printf(“%c\n”,字符->字符);
//这只是为了输出结果
回来
}

那么你的问题是什么?我不知道如何解决这个问题,我只对指针有基本的了解,但因为期末考试就要到了,老师们决定这是一个大困难的时刻。所以在这里,我试图理解我应该做什么以及为什么。永远不要把指针隐藏在typedef后面,它除了使代码更难阅读之外,什么都做不了。谢谢,你的代码工作得很好。只是一个问题,为什么它显示的字母是相反的?例如,它不会显示h:1E:1L:2O:1,而是从那个时候开始移动到l,以此类推。我在开头添加了新节点,即名称[0]是最后一个节点,名称[strlen(name)-1]是第一个节点。如果要按相同的顺序打印,可以将for循环修改为for(i=0;name[i]!='\0';i++);而(i--)createnode(名称[i],lst_ptr);或者在“报告”功能中,您可以开始将节点从最后一个节点打印到第一个节点。