鱼C论坛

 找回密码
 立即注册
查看: 2583|回复: 8

函数调用问题

[复制链接]
发表于 2019-6-26 21:01:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#define LIM 10
#define SIZE 81

void menu(void);
void original(char *str [], int num);

int main(void)
{
        char input[LIM][SIZE];
        char ch;
        int ct = 0;

        printf("Input up to %d lines.\n", LIM);
        while (ct < LIM && gets(input[ct]) != NULL && input[ct][0] != '\n')
        {
                //puts(input[ct]);
                ct++;
        }

        menu();
        while ((ch = getchar()) != 'q')
        {
                switch (ch)
                {
                case 'a':
                        original(input,LIM);
                        break;
                case 'b':
                case 'c':
                case 'd':
                default:
                        printf("q to quit,not other words.\n");
                        break;
                }
                menu();
        }
        puts("Bye!");

        getchar();
        return 0;
}

void menu(void)
{
        printf("You have five choices.\n");
        printf("a、Print a list of source strings.\n");
        printf("b、Print strings in order in ASCII.\n");
        printf("c、Print strings in incremental order of length.\n");
        printf("d、Print the string by the length of the first word in the string.\n");
        printf("q、QUIT.\n");

}

void original(char *str [],int num)
{
        int i;
        //char *pt[LIM];
        for (i = 0; i < num; i++)
        {
        //        pt[i] = str[i];
                puts(str[i]);
        }
}



puts的时候会出现0x6C6C6568 时发生访问冲突。这是为什么呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-26 21:25:01 | 显示全部楼层
#include <stdio.h>
#define LIM 2
#define SIZE 81

void menu(void);
void original(char *str [], int num);

int main(void)
{
        char input[LIM][SIZE] = {0};
        char ch;
        int ct = 0;

        printf("Input up to %d lines.\n", LIM);
        while (ct < LIM && gets(input[ct]) != NULL && input[ct][0] != '\n')
        {
                //puts(input[ct]);
                ct++;
        }


        menu();
        while ((ch = getchar()) != 'q')
        {
                switch (ch)
                {
                case 'a':
                        original(input,LIM);
                        break;
                case 'b':
                case 'c':
                case 'd':
                default:
                        printf("q to quit,not other words.\n");
                        break;
                }
                menu();
        }
        puts("Bye!");

        getchar();
        return 0;
}

void menu(void)
{
        printf("You have five choices.\n");
        printf("a、Print a list of source strings.\n");
        printf("b、Print strings in order in ASCII.\n");
        printf("c、Print strings in incremental order of length.\n");
        printf("d、Print the string by the length of the first word in the string.\n");
        printf("q、QUIT.\n");

}

void original(char *str[], int num)
{
        int i;
        char (*pt)[SIZE] = str;
        for (i = 0; i < num; i++)
        {
        //        pt = str;
                puts(pt[i]);
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-26 22:29:31 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-26 22:50:16 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-26 22:51:31 | 显示全部楼层

那我为什么会出现这个问题?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-26 22:55:33 | 显示全部楼层
本帖最后由 jackz007 于 2019-6-26 23:01 编辑

      楼主,贴代码的时候,点击一下页面上工具栏中标有 "<>" 的按钮,把全部代码贴到新弹出的窗口中,这样,可以避免丢码,你贴出的代码出现斜体,那是因为你代码中出现的 [ i ] 一律被当成页面语言的斜体指令来解释了,这样,所有的 [ i ] 也就全部被过滤掉了。 如果贴成代码,便可以避免出现此问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-26 22:56:12 | 显示全部楼层
hickttye 发表于 2019-6-26 22:51
那我为什么会出现这个问题?


我测了你原来的代码并不会报错,只是只能输出第一个数据,加上下标后,即str【i】会报错,
因为puts()函数它判断不出你传进去的是个地址还是个什么,
所以要转换为固定长度的数组指针。然后再循环输出就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-26 23:06:54 | 显示全部楼层
newu 发表于 2019-6-26 22:56
我测了你原来的代码并不会报错,只是只能输出第一个数据,加上下标后,即str【i】会报错,
因为puts() ...

我试过了你的办法,但是只有第一行的字符串能正确输出,第二行就就变了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-26 23:10:33 | 显示全部楼层
hickttye 发表于 2019-6-26 23:06
我试过了你的办法,但是只有第一行的字符串能正确输出,第二行就就变了


我改为3行测试了下,没问题啊
1561561770.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表