2017-09-26 207 views
-2

我正在尝试编写一个程序来计算给定字符串的每个字母中的字母,并将计数附加到该字词上。如何在C中将字符数组重置为0/NULL?

示例 - I/P:蛋糕O/P:The3 cake4

以我的逻辑,我想重置命名的类型整数(完成)&的icnt计数器变量和命名的temp阵列键入字符(不工作)。 我做了一些关于StackOverflow的研究,并实现了memset来重置,但看起来它不适合我!

如果有错误,请更正我的程序。

#include<stdio.h> 
#include<string.h> 
void fun(char*,char*); 

char ch[50]; 
char temp[10]={0}; 

int main()              
{                

     printf("Enter string :"); 
     scanf("%[^'\n']s",&ch); 

     fun(ch,temp); 
} 

void fun(char *ptr,char *tptr) 
{ 
     int icnt = 0; 

     while(*ptr!='\0') 
     { 
       if(*ptr!=' ') 
       { 
         *tptr = *ptr; 
         tptr++; 
         icnt++; 
       } 
       else if(*ptr==' ') 
       { 
         printf("In IF bcoz of space\n"); 

         printf("%s %d\n",temp,icnt); 
         icnt = 0; 
         //temp[10] = {0};  //* 
         memset(temp,0,10);  //* 
         //Tried reseting in both the above ways * 
       } 

       ptr++; 

     } 

     if(*ptr=='\0') 
     { 
       printf("%s%d\n",temp,icnt); 
     } 


} 

输出上述程序的:The34

看起来像什么也不得到存储在所述第二时间在临时变量

的例外输出:The3 cake4

+0

fun函数将找到的单词写入'* tptr'中,而不是直接写入'temp'变量。所以一旦你发现一个词,你应该'tptr'重新初始化为'&temp'。 –

回答

1
#include<stdio.h> 
#include<string.h> 
void fun(char*,char*); 

char ch[50]; 
char temp[10]={0}; 

int main()              
{                

     printf("Enter string :"); 
     scanf("%[^'\n']s",ch); 

     fun(ch,temp); 
} 

void fun(char *ptr,char *tptr) 
{ 
     int icnt = 0; 

     while(*ptr!='\0') 
     { 
       if(*ptr!=' ') 
       { 
         *tptr = *ptr; 
         tptr++; 
         icnt++; 
       } 
       else if(*ptr==' ') 
       { 
        printf("In IF bcoz of space\n"); 

        printf("%s %d\n",temp,icnt); 
        icnt = 0; 
        //temp[10] = {0};  //* 
        memset(temp,0,10); 
        tptr = temp; /* The tptr was not reinitialised to point to base address */ 
        //Tried reinitialization in both the above ways * 
       } 

       ptr++; 

     } 

     if(*ptr=='\0') 
     { 
      printf("%s%d\n",temp,icnt); /* Here you are printing from the base address.*/ 
     } 
} 

打印是从阵列的基地址完成并且在重新初始化之后tptr未设置为指向阵列的基地址。代码附加应该工作正常。

+0

是的指针没有重新初始化...您的更正对我有效。谢谢! –

2

printf("%s %d\n",temp,icnt);

temp[10] = {0};

你的函数需要temp作为参数,直接访问该全局变量。这不是一个错误,但它没有任何意义:只有当tptr参数具有一定的值时,该函数才有效。那么为什么要使用论证呢?

temp[10] = {0};

memset(temp,0,10);

因为你的功能将覆盖阵列与线*tptr = *ptr内容是 neccessary到temp阵列全天初始化!

相反,您只需确保数组中的最后一个值为零。你该做的方式如下:

*tptr = 0; 
printf(...,temp,...); 

理论上你也可以使用memset但这时并不需要,并会采取更多的计算时间。

输出上述方案:The34

你的程序增量tptrtptr++),但它永远不会重新套了!

因此temp阵列具有以下内容:

/* Before the first printf: */ "The\0\0\0\0\0\0\0" 
/* After memset: */ "\0\0\0\0\0\0\0\0\0\0" 
/* Before the second printf: */ "\0\0\0cake\0\0\0" 

如果你的输入超过10个字符你的程序甚至更长,可能会崩溃,因为它写入到内存不允许写。因为temp数组的第一个字符是NUL

printf会写一个空字符串,第二次......

请纠正我的程序是否有错误。

我会做如下修改:

我会用两种不同的变量tptr

void fun(char *ptr,char *output) 
{ 
    char *tptr = output; /* here */ 

而是在功能指引temp我将引用的说法,我从来不会修改。 printf之前,我将在NUL字节写入到阵列的末尾,printf后我会重新写指针(tptr):

*tptr = *ptr; /* This remains tptr; does not become output */ 
*tptr++; /* This also */ 

... 

*tptr = 0; /* This is new: Ensure the last byte is NUL */ 
printf(...,output,...); /* Not test, but output */ 
tptr = output; /* This is new: Reset the write pointer */ 
/* memset(...) - No longer needed */ 
+0

应该建议使用'sizeof' –

+0

@BasileStarynkevitch如果用户传递'temp'作为参数(并且不直接访问数组),他或她不能使用'sizeof'来获取数组的大小('sizeof(output)'将会返回指针的大小,而不是指针指向的数组的大小)。 –

+0

当'temp'是一个像char temp [10];' –

0

你后面添加“INCT = 0;”指令:“tptr = temp;”因为如果你不添加它,你将从表“temp”的内存空间中出去。而且你还要在“printf(”%s%d \ n“,temp,icnt);”之前加上“* tptr ='\ 0'”。

相关问题