2012-08-05 61 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