指针数组也能通过scanf()或gets()函数赋值字符串?

时间:2021-12-16 01:47:27
#include <stdio.h>
#include <string.h>

main()
{
int length();

int len;
char *str[20];//这里为什么要被定义为指针数组呢?
printf("Input string:");
scanf("%s",str);
len=length(str);
printf("The length of string is %d.\n",len);
}

length(char *p)
{
int n;
n=0;
while(*p!='\0')
{
n++;
p++;
}
return(n);
}

以及代码在Visual C++6.0中通过编译,能够执行,且0 error,0 warning

8 个解决方案

#1


scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

#2


准确地说,scanf只要是指针都能当作参数传进去,它不检查那玩意逻辑上有没有问题,这些细节需要写代码的人自己把握

#3


char *str[20];
是字符指针的数组,而字符指针是指向字符串的。并且你输入的形式如下:
scanf("%s",str);输入的是字符,所以要那样声明。

#4


引用 1 楼 mstlq 的回复:
scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

完全通过运行,没提示有error,没提示有warning,也没有弹出“遇到问题需要关闭”对话框

但如果把sanf()改为gets(),编译时就会提示:
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[20]'
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4024: 'gets' : different types for formal and actual parameter 1

#5


引用 4 楼 huangyiyun 的回复:
引用 1 楼 mstlq 的回复:
scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

完全通过运行,没提示有error,没提示有warning,也没有弹出“遇到问题需要关闭”对话框

但如果把sanf()改为gets(),编译时就会提示:
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[20]'
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4024: 'gets' : different types for formal and actual parameter 1



str是一个指针数组,而且分配的空间是20,如果将它解释成char *的话,就指向内存中一块80字节长的已分配区域,想你那样简单地用当然没问题╮(╯_╰)╭

实际上,正常情况下,人们经常这样用指针数组……
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define WIDTH 5

main()
{
    int len;
    char *str[20];
    for(int i=0; i<20;i++)
    {
            str[i]=(char*)malloc(sizeof(char)*WIDTH);
    }
    /*在这里对str二维数组进行必要操作*/
    for(int i=0; i<20;i++)
    {
            free(str[i]);
    }
    system("pause");
}




如果在上面注释的地方用上楼主那些scanf("%s",str);之类的代码……
运行时遇到free语句就会异常的╮(╯_╰)╭

#6


5楼的意思是不是说像楼主那样的用法是违规的???

#7


嗯,这种做法应该杜绝,因为代码多了之后,这样的东西容易引起bug……

其实不要说char *str[20]了,就算是
int str[20];
int *str[20];
这样的定义,放到楼主的代码里也是应该可以0警告0错误,使用时不报错的……
只是,有人这样用吗^_^?

#8


引用 7 楼 mstlq 的回复:
嗯,这种做法应该杜绝,因为代码多了之后,这样的东西容易引起bug……

其实不要说char *str[20]了,就算是
int str[20];
int *str[20];
这样的定义,放到楼主的代码里也是应该可以0警告0错误,使用时不报错的……
只是,有人这样用吗^_^?

#1


scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

#2


准确地说,scanf只要是指针都能当作参数传进去,它不检查那玩意逻辑上有没有问题,这些细节需要写代码的人自己把握

#3


char *str[20];
是字符指针的数组,而字符指针是指向字符串的。并且你输入的形式如下:
scanf("%s",str);输入的是字符,所以要那样声明。

#4


引用 1 楼 mstlq 的回复:
scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

完全通过运行,没提示有error,没提示有warning,也没有弹出“遇到问题需要关闭”对话框

但如果把sanf()改为gets(),编译时就会提示:
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[20]'
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4024: 'gets' : different types for formal and actual parameter 1

#5


引用 4 楼 huangyiyun 的回复:
引用 1 楼 mstlq 的回复:
scanf没有很强的类型检查,编译时能过的……
但逻辑上不通……
运行时可能会挂

完全通过运行,没提示有error,没提示有warning,也没有弹出“遇到问题需要关闭”对话框

但如果把sanf()改为gets(),编译时就会提示:
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[20]'
D:\C\exercise\chapter10\10.6-B1.c(11) : warning C4024: 'gets' : different types for formal and actual parameter 1



str是一个指针数组,而且分配的空间是20,如果将它解释成char *的话,就指向内存中一块80字节长的已分配区域,想你那样简单地用当然没问题╮(╯_╰)╭

实际上,正常情况下,人们经常这样用指针数组……
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define WIDTH 5

main()
{
    int len;
    char *str[20];
    for(int i=0; i<20;i++)
    {
            str[i]=(char*)malloc(sizeof(char)*WIDTH);
    }
    /*在这里对str二维数组进行必要操作*/
    for(int i=0; i<20;i++)
    {
            free(str[i]);
    }
    system("pause");
}




如果在上面注释的地方用上楼主那些scanf("%s",str);之类的代码……
运行时遇到free语句就会异常的╮(╯_╰)╭

#6


5楼的意思是不是说像楼主那样的用法是违规的???

#7


嗯,这种做法应该杜绝,因为代码多了之后,这样的东西容易引起bug……

其实不要说char *str[20]了,就算是
int str[20];
int *str[20];
这样的定义,放到楼主的代码里也是应该可以0警告0错误,使用时不报错的……
只是,有人这样用吗^_^?

#8


引用 7 楼 mstlq 的回复:
嗯,这种做法应该杜绝,因为代码多了之后,这样的东西容易引起bug……

其实不要说char *str[20]了,就算是
int str[20];
int *str[20];
这样的定义,放到楼主的代码里也是应该可以0警告0错误,使用时不报错的……
只是,有人这样用吗^_^?