2017-05-08 43 views
2

,并感谢您的时间。我提前道歉,我是C编程新手,在堆栈溢出时发布。我可能遗漏的任何信息和问题请您提出。需要在正确的方向“中指出”开头的C编程

我有这个实验我的工作我的课,我无法理解可怕的指针是如何运作的。首先我会解释实验室的指示。

首先,我创建的200个字的阵列具有30 + 1为空一最大长度。 接下来,我需要创建的通话功能包括:

  1. 一个读取函数,用于将文件中的单词读入数组中。我必须使用fopen和fscanf函数。
  2. 的函数把字符串转换使用每个字符的ASCII码为小写。 (必须使用指针)
  3. 函数返回一个字符串的长度(不能使用strlen函数并且必须使用指针)
  4. 一个带有三个参数的函数(数组的单词,数组中的单词和一个int长度)。函数返回数组中与int长度匹配的数字。
  5. 甲打印功能打印所述阵列中的所有词语的

我使用的IDE是Dev C++,因此我也一直使用netbeans。 我只尝试创建读取,打印和转换为小写函数。我第一次尝试读取文件并在main中打印数组。我读的是我创建的文件,它包含如下正好短短的一句话:

  • 有这里这么多的话

编辑 - 更新主代码当前与容器内工作小写环主要。

#define rows 200 //How many words allowed in array. 
#define cols 31  //How many characters allowed for each word. 

void lowercase(char* words, int count); 
int read(char (*words)[cols]); 
void print(char (*words)[31], int count); 

int main(int argc, char *argv[]){ 
    char words[rows][cols]; 
    int i, j; 
    int count = read(words); 

    print(words, count); 
/* 
    //make words lowercase 
    for(i = 0;i<count;i++){ 
     for(j = 0;j<cols;j++){ 
      if(words[i][j]!=0){ 
       if(words[i][j]<91 && words[i][0]>64) 
        words[i][j] = words[i][j]+32; 
      } 
     } 
    }*/ 

    for(i = 0;i < count;i++){ 
     lowercase(*words+i, count); 
    } 
    print(words, count); 

    return 0; 
} 

的代码写得不好,管理不善,我只是试图让一切先那么这将是更合适的工作。第一个printf输出出来如何应:

数组[0]:有

阵列[1]:ARE

阵列[2]:所以

阵列[3]:MANY

数组[4]:词语

阵列[5]:在

阿拉Y [6]:HERE

然后,打印功能我有打印出字阵列正确,但它包括用于每个字,而不是仅仅在字中的所有30位。这是如何写它我需要改变它。

void print(void *array, int SIZE){ 
    int i, 
     j; 

    char *charArray = (char *) array; 

    for(j = 0; j < SIZE; j++){ 
     for(i = 0; i < SIZE; i ++){ 
      printf("%c ", charArray[j*SIZE + i]); 
     } 
     printf("\n"); 
    } 
} 

我创建的tolower函数部分地将每个单词的第一个字母转换为小写。现在它已经破产,不记得我改变了什么。

EDIT-更新的小写函数。在主要小写工作正好,但与此功能,它不会将所有的单词转换为小写,它停止在第三个字,其余是相同的。

void lowercase(char *words, int count){ 
    int j; 

    for(j = 0;j<cols;j++){ 
     if(words[j]!=0){ 
      if(words[j]<91 && words[j]>64) 
       words[j] = words[j]+32; 
     } 
    } 
} 

我试图在主读取的代码移动到其自身的功能也试图模仿与指针的打印代码,但是当我运行它,它摊位和exe文件停止工作窗口的命令提示符弹出。 IDE中没有错误或警告。

int read(void *array){ 
    FILE *file; 
    int i, 
     j; 

    char *words = (char *) array; 
    file = fopen("words.txt", "r"); 
    //STORE IN ARRAY 
    for(i=0;i<7;i++) 
     fscanf(file,"%s", words[i]); 
} 

如果您还没有弄清楚我不知道何时或如何使用指针或地址。我在12个小时内基本上都学过C语言,在我看来,没有足够的时间学习语言,特别是要高效地理解语言。任何帮助将不胜感激。谢谢。

+0

为什么要用'无效*'代替'字符字[] [31];'或'字符(*话)[31];'? – BLUEPIXY

+0

我不是很有趣,但这个视频很棒。帮助我更好地理解它。 https://www.youtube.com/watch?v=mnXkiAKbUPg –

+0

@BLUEPIXY它似乎是唯一的方法来让它工作我会得到一堆使用char的错误。不理解指针或错误C显示不起作用。我的教授希望我使用指针无数组括号。 – Cherve

回答

2

通过将2维阵列向下投射到char*,您已经丢失了一些信息。如果你在正确读取的话,那么在内存中,您阵列可能是这样的:

0   10  20  30 
|.........|.........|.........|. 
There 
ARE 
so 
MANY 
words 
in 
HERE 

要访问words[1]编译器自动从数组开始偏移31个字节。

您的问题是,在将words转换为char*之后,编译器不再知道二维结构,现在words[1]只会从数组的开头偏移1个字节。

一个简单的解决方案是重新定义你的read功能:

int read(char words[][31]) 
{ 
    FILE *file; 
    int i, j, count = 0; 
    file = fopen("words.txt", "r"); 
    for (i=0; i<7; i++) 
    { 
     count += (1 == fscanf(file, "%s", words[i])); 
    } 
    return count; 
} 

现在编译器知道了words[i]内存步幅大小为31个char值。

类似的事情与print

void print(char words[][31], int count) 
{ 
    int i; 
    for(i = 0; i < count; i ++) 
    { 
     printf("%s\n", words[i]); 
    } 
} 
2

修复这样的:

#include <stdio.h> 
#include <stdlib.h> 

//Stringification 
#define S_(n) #n 
#define S(n) S_(n) 

//Information to be shared across the whole area 
#define MAX_ROWS 200 
#define MAX_WORD_LENGTH 30 
#define COLS (MAX_WORD_LENGTH + 1) 

#define DATA_FILE "words.txt" 

int read(void *array); 
void print(void *array, int rows); 

int main(void){ 
    char words[MAX_ROWS][COLS]; 
    int rows; 

    rows = read(words); 
    print(words, rows); 

    return 0; 
} 

int read(void *array){ 
    FILE *file = fopen(DATA_FILE, "r"); 
    if(file == NULL){ 
     perror("fopen:"); 
     exit(EXIT_FAILURE); 
    } 
    char *words = array; 
    int rows; 
    for(rows = 0; rows < MAX_ROWS; ++rows, words += COLS){ 
     if(fscanf(file, "%" S(MAX_WORD_LENGTH) "s", words) == EOF) 
      break; 
    } 
    fclose(file); 
    return rows; 
} 

void print(void *array, int rows){ 
    char *words = array; 
    for(int r = 0; r < rows; ++r, words += COLS){ 
     printf("Array [%d]: %s\n\n", r, words); 
    } 
} 
+0

@ M.M确实。谢谢 – BLUEPIXY

+0

使用全局信息不是一种好方法,因为它会影响功能的独立性。 – BLUEPIXY