hickttye 发表于 2019-6-26 21:01:53

函数调用问题

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

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

int main(void)
{
        char input;
        char ch;
        int ct = 0;

        printf("Input up to %d lines.\n", LIM);
        while (ct < LIM && gets(input) != NULL && input != '\n')
        {
                //puts(input);
                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;
        for (i = 0; i < num; i++)
        {
        //        pt = str;
                puts(str);
        }
}


puts的时候会出现0x6C6C6568 时发生访问冲突。这是为什么呢?

newu 发表于 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 = {0};
      char ch;
      int ct = 0;

      printf("Input up to %d lines.\n", LIM);
      while (ct < LIM && gets(input) != NULL && input != '\n')
      {
                //puts(input);
                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) = str;
      for (i = 0; i < num; i++)
      {
      //      pt = str;
                puts(pt);
      }
}





hickttye 发表于 2019-6-26 22:29:31

newu 发表于 2019-6-26 21:25


?

newu 发表于 2019-6-26 22:50:16

hickttye 发表于 2019-6-26 22:29
?

测一下先{:10_266:}

hickttye 发表于 2019-6-26 22:51:31

newu 发表于 2019-6-26 22:50
测一下先

那我为什么会出现这个问题?

jackz007 发表于 2019-6-26 22:55:33

本帖最后由 jackz007 于 2019-6-26 23:01 编辑

      楼主,贴代码的时候,点击一下页面上工具栏中标有 "<>" 的按钮,把全部代码贴到新弹出的窗口中,这样,可以避免丢码,你贴出的代码出现斜体,那是因为你代码中出现的 [ i ] 一律被当成页面语言的斜体指令来解释了,这样,所有的 [ i ] 也就全部被过滤掉了。 如果贴成代码,便可以避免出现此问题。

newu 发表于 2019-6-26 22:56:12

hickttye 发表于 2019-6-26 22:51
那我为什么会出现这个问题?

我测了你原来的代码并不会报错,只是只能输出第一个数据,加上下标后,即str【i】会报错,
因为puts()函数它判断不出你传进去的是个地址还是个什么,
所以要转换为固定长度的数组指针。然后再循环输出就可以了。

hickttye 发表于 2019-6-26 23:06:54

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

我试过了你的办法,但是只有第一行的字符串能正确输出,第二行就就变了

newu 发表于 2019-6-26 23:10:33

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

我改为3行测试了下,没问题啊
页: [1]
查看完整版本: 函数调用问题