LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 660|回复: 4

一个很莱的C程序

[复制链接]
发表于 2003-10-13 01:28:36 | 显示全部楼层 |阅读模式
请帮忙,这么简单的我都不懂。都不好意思问了。
# gcc -c twof2.c
twof2.c: In function `main':
twof2.c:3: warning: initialization from incompatible pointer type
# cat twof2.c
main()
{
int a[2][3],*p=a;
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
  scanf("%d",p+i*3+j);
for(i=0;i<2;i++)
  {
     printf("\n");
     for(j=0;j<3;j++)
      printf("%10d",*(p+i*3+j));
  }
}

#
发表于 2003-10-13 01:45:44 | 显示全部楼层
只是个警告,因为a是int [2][3],编译器告诉你这里可能有问题,如果你知道自己确实要这么做就没有问题,可以不理它。
从你的程序来看没什么问题,不过你既然都用了两个循环,干脆用a[j]算了,比指针好看多了
 楼主| 发表于 2003-10-13 10:18:11 | 显示全部楼层
谢谢版主,但学到指针,要做些练习的。
顺便问一下,指针的真正作用是节省内存吗?
发表于 2003-10-13 10:59:26 | 显示全部楼层

不全是

不全是,还提高速度和增强功能
发表于 2003-10-13 12:23:22 | 显示全部楼层
I am also learning C, and I am not a professional.  The following is one of the homework for last week.   It may help a bit on how pointer works.
This program check a list of word from a file for palindromes (正讀反讀都一樣的詞).
Pointers are very useful when pass data in between functions.
Also give me some comment please.



  1. #include <stdio.h>

  2. int readFile(char *, char **);
  3. void palindromeCheck(char *);

  4. int main (void)
  5. {
  6.     char *palindromeList, filename[255] = {'\0'};

  7.     /* ask user for the filename */
  8.     printf("\nPlease enter the filename of your Palindrome list.\n"
  9.            ": ");
  10.     scanf("%s", filename);

  11.     /* read the word list into memory */
  12.     if (readFile(filename, &palindromeList))
  13.        exit(1);
  14.     putc('\n', stdout);

  15.     /* chck each word for palindrome */
  16.     palindromeCheck(palindromeList);
  17.    
  18.     return 0;
  19. }

  20. void palindromeCheck(char *buffer)
  21. {
  22.      int palindrome;
  23.      char *firstChr, *lastChr, *endLine;

  24.      firstChr = endLine = buffer;

  25.      /* loop until end of file */
  26.      while (*firstChr != EOF)
  27.            {
  28.            palindrome = 1;
  29.            /* find the last character */
  30.            while (*endLine != '\n')
  31.                  putc(*(endLine++), stdout);
  32.            lastChr = endLine - 1;
  33.            /* palindrome compair */
  34.            while ( (firstChr != lastChr) && ((firstChr + 1) != lastChr) )
  35.                  if (*(firstChr++) != *(lastChr--))
  36.                     palindrome = 0;
  37.            firstChr = ++endLine;

  38.            /* display result */
  39.            printf(" %s a palindrome\n", (palindrome)? "is":"is not");
  40.            }
  41. }

  42. int readFile(char *filename, char **buffer)
  43. {
  44.     unsigned int count = 0;
  45.     FILE *fptr;
  46.    
  47.     if ( (fptr = fopen(filename, "r")) == NULL )
  48.        {
  49.        fprintf(stderr, "Error Openning Source Code File %s!\n", filename);
  50.        return 1;
  51.        }
  52.    
  53.        /* get the size of the file for memory allocation.
  54.         * read the file onec and count the character make
  55.         * the code protable.*/
  56.         while (getc(fptr) != EOF)
  57.               count++;
  58.         rewind(fptr);

  59.        /* allocate memory */
  60.        *buffer = malloc(count * sizeof(char));

  61.        /* read in the word list and echo them to stdout */
  62.        puts("\nReading Palindrome List From File:");
  63.        count = 0;
  64.        while ( (*(*buffer + count) = getc(fptr)) != EOF)
  65.               putc(*(*buffer + count++), stdout);
  66.       
  67.        fclose(fptr);
  68.        return 0;
  69. }
复制代码


Data File "test.dat"

  1. anna
  2. pip
  3. +-*-+
  4. world
  5. sky
  6. same
  7. ga1ag
复制代码


Run Log:
Please enter the filename of your Palindrome list.
: test.dat
                                                                              
Reading Palindrome List From File:
anna
pip
+-*-+
world
sky
same
ga1ag
                                                                              
anna is a palindrome
pip is a palindrome
+-*-+ is a palindrome
world is not a palindrome
sky is not a palindrome
same is not a palindrome
ga1ag is a palindrome
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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