这个程序运行时总是停止工作,搞不清为什么

C语言 码拜 8年前 (2016-04-24) 901次浏览
#include <stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
int n,l,t=0,i=0;
char s[200];
scanf(“%d”,&n);
int* ptd[100];
while(i<n)
{
scanf(“%s”,s);  //gets(s);
l = strlen(s);
for(t=0;t<l;t++)
{
if(“A”<=s[t]&&s[t]<“Z”)
s[t] = s[t]+32;
else if(“a”<=s[t]&&s[t]<“z”)
s[t] = s[t]-32;
}
ptd[i]=”s”;
i++;
}
for(t=0;t<n;t++)
printf(“%s”,*ptd[t]);
return 0;
}
解决方案

10

#include <stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
int n,l,t=0,i=0;
char s[200];
scanf(“%d”,&n);
int* ptd[100];//这是个指针数组,你没给出具体的空间,就是说没初始化,可以写 int ptd[100][200];
while(i<n)
{
scanf(“%s”,s);  //gets(s);
l = strlen(s);
for(t=0;t<l;t++)
{
if(“A”<=s[t]&&s[t]<“Z”)
s[t] = s[t]+32;
else if(“a”<=s[t]&&s[t]<“z”)
s[t] = s[t]-32;
}
ptd[i]=”s”;//这里你他们赋值了。
i++;
}
for(t=0;t<n;t++)
printf(“%s”,*ptd[t]);
return 0;
}

10

楼上正解你使用了一个 指针数组,然后给指针进行了赋值,类型是从char转换成int* 这里都三符合语法的,但是你*ptd[t]这里对一个非法地址进行*操作,是不可以的

10

   33 #include <stdio.h> 
   32 #include<string.h> 
   31 #include<malloc.h> 
   30 int main()         
   29 {                  
   28     int n, l, t=0, i=0;
   27     char s[200];   
   26     scanf("%d",&n);
   25     char *ptd[100];    //here you should use char for the follow using
   24                    
   23     while(i < n && i < 100)    //the value could not cross the value of 100
   22     {              
   21     |   scanf("%s",s);  //gets(s);
   20     |   l = strlen(s);
   19     |   for(t = 0; t < l; t++)
   18     |   {          
   17     |   |   if("A" <= s[t] && s[t] <= "Z")    //include capital "Z"
   16     |   |   |   s[t] = s[t]+32;
   15     |   |   else if("a" <= s[t] && s[t] <= "z")    //include lowercase letter "z"
   14     |   |   |   s[t] = s[t]-32;
   13     |   }          
>> 12     |   ptd[i] = malloc(sizeof(128)); 
   11     |   if (NULL == ptd[i]) {
   10     |   |   fprintf(stderr, "malloc error!\n");
    9     |   |   return -1;
    8     |   }          
    7     |   //ptd[i] = s;    //pointer the first address of the string called s
    6     |   memcpy(ptd[i], s, l+1);
    5     |   i++;       
    4     }              
    3     for(t = 0; t < n; t++)
    2     |   printf("%s\n", ptd[t]);    //here use the address because the format %s
    1     return 0;      
  34  }                                                                                                                                                                                                      
  ~          

10

由于本人也是菜鸟,不太清楚题主这段程序的实现功能。所以将题主的程序作了点修改,仅供题主参考:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main()
{
    int n,l,t=0,i=0;
    char s[200];
    char* ptd[100];
    scanf("%d",&n);
    for (t=0; t<100; t++)
        ptd[i] = (char *)malloc(100 * sizeof(char));
    while(i<n)
    {
        scanf("%s",s);
        l = strlen(s);
        for(t=0;t<l;t++)
        {
            if("A"<=s[t]&&s[t]<"Z")
            s[t] = s[t]+32;
            else if("a"<=s[t]&&s[t]<"z")
            s[t] = s[t]-32;
        }
        ptd[i]=s;
        printf("ptd[%d] = [%s]\n",i,ptd[i]);
        i++;
        memset(s, 0x00, sizeof(s));
    }
    for (t=0; t<100; t++)
        free(ptd[i]);
    return 0;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明这个程序运行时总是停止工作,搞不清为什么
喜欢 (0)
[1034331897@qq.com]
分享 (0)