2012-08-05 72 views
0

我想学习C,并写了一个简单的程序,接受来自用户的字符串并打印出来。你愿意为我的做法提出任何建议吗?我需要很好地学习它。所以请帮助我改善自己。 这里去我的代码:提前学习动态分配

//Dynamic Array Allocation 
#include <stdio.h> //this is a c code 
#include <conio.h> //for using getch() 
#include <stdlib.h> //for using malloc,realloc, and free 

void createACopy(char * copyTo,char * copyFrom, int length) //creates copy 
{ 
    for(int i = 0; i < length; i++) //loof for 'length' times 
    { 
     copyTo[i] = copyFrom[i]; 
    } 
} 

void main() 
{ 
    printf("Please enter a string\n"); 
    char inputChar; //a characted input by user 
    int inputLength = 0; //holds the length of characters input so far 
    char * userInput; //a pointer that points to the beginnning of the user input 
    userInput = (char *)malloc(sizeof(char)); //dynamically assign a single character size memory to the pointer 
    if (userInput == NULL) //if malloc could not find sufficient memory 
    { 
     free (userInput); //free the memory 
     puts ("Error Allocating memory"); //print error message 
     exit (1); //exit the program 
    } 
    do{ //keep looping till the user hits 'Enter' key 
    inputChar = getch(); //get the character keyed in by user in inputChar variable 
    if(inputChar ==char(8)) //if user hits backspace 
    { 
     inputLength--; //decrease the length of user input by 1 
     continue; //continue and look for next character 
    } 
    char * storeOldInputHere = (char *) malloc(inputLength+1); //dynamically find a memory location of size 'inputLenght' 
    if (storeOldInputHere == NULL) //if malloc could not find sufficient memory 
    { 
     free (storeOldInputHere); 
     puts ("Error Allocating memory for copying the old input"); 
     exit (1); 
    } 
    createACopy(storeOldInputHere,userInput,inputLength); //store the old Input here because realloc might give us a different location altogether. 
    userInput = (char *) realloc(userInput,inputLength+2); //now after we got a new character, reallocate memory. 
    if (userInput == NULL) //if realloc could not find sufficient memory 
    { 
     free (userInput); 
     puts ("Error Reallocating memory"); 
     exit (1); 
    } 
    createACopy(userInput, storeOldInputHere,inputLength); //Copy back the original input string to the newly allocated space again. 
    userInput[inputLength] = inputChar; //append the new character user inserted. 
    free (storeOldInputHere); //free the storeOldInputHere 
    inputLength ++; //increment the length counter by 1 
    }while(inputChar != char(13)); //keep looping untill user hits 'Enter' key 
    userInput[inputLength] = '\0'; //append a null charater at the end of the string 
    printf("\nyou entered %d characters",inputLength); 
    printf("\nyou entered: %s\n",userInput); 
    free(userInput); //free the userInput 
} 

感谢

回答

0

很多东西是改进,但一对夫妇的建议现在:

首先,在C做什么用炭炭是乏味和容易出错,如果你能避免它,你应该。通常我更喜欢fprintf放置,并且readline或getline over getch。

其次,在使用malloc时,您应该始终将它传递给您正在使用的数据类型大小的一个因子。

例如

size_t counter = 0; 
//then some stuff happens in your code and counter gets set to some value...say 24 
char * charspace = (char *) malloc(sizeof(char)*counter); 
// charspace is now an array with space for 24 chars. 

这也是一个很好的做法,将错误检查封装在一个函数中,所以你不必担心它。

例如

void *emalloc(size_t n) { 
void *p; 
p = malloc(n); 
if (p == NULL) { 
    fprintf(stderr, "emalloc of %u bytes failed", (unsigned int) n); 
    exit(1); 
} 
return p; 
} 

希望有帮助。

+0

非常感谢matthewdavidson。成为这样的团队的一员真是太好了。我相信我会在这里学到很多东西。 – teachMyself 2012-08-06 18:22:26