2012-11-07 2 views
0

J'essaie de modifier un programme précédent que j'ai écrit en utilisant la notation de pointeur pur. C'est un programme qui génère une chaîne aléatoire de 40 majuscules, prend une entrée de 20 lettres majuscules, et la saisie d'un caractère. Le programme remplace les caractères récurrents dans la chaîne générée avec le caractère entré. En ce moment, j'essaie de passer les paramètres de la chaîne générée aléatoirement afin que je puisse y accéder dans la deuxième fonction et ne peux pas comprendre comment le faire.Je n'arrive pas à comprendre comment passer correctement les paramètres avec la notation de pointeur pur

Merci pour votre aide.

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

/* Function prototypes */ 
void fillS1(char * x); 

void fillS2(char * x, char * y, char z); 

void strFilter(char * a, char * b, char c); 

int main(int argc, const char * argv[]) 
{ 

char s1[41]; 
char s2[21]; 
char x = 0; 

char * pointerToS1; 
char * pointerToS2; 

pointerToS1 = s1; 
pointerToS2 = s2; 

fillS2(pointerToS2, pointerToS1, x); 

return 0; 
} 

/* Function to generate a random string of 40 uppercase letters */ 
void fillS1(char * randomlyGeneratedPointer) 
{ 
char randomlyGeneratedArray[41]; 

randomlyGeneratedPointer = randomlyGeneratedArray; 

int i; 

for (i = 0; i < 40; i++) { 
    *(randomlyGeneratedPointer + i) = 'A' + rand() % 26; 
} 


} 

/* Function to get user input of characters */ 
void fillS2(char * userStringPointer, char * randomStringPointer, char  replacementCharacter) 
{ 

char userstring[21]; 
char randomString[41]; 
char copyString[42]; 
//char * pointerToCopyString = copyString; 

userStringPointer = userstring; 



int i = 0; 
int n = 0; 
int lowercaseCheck = 0; 

char loopContinue = 0; 




fillS1(randomStringPointer); //here is the function call for the randomly generated  string. 



printf("This is the random string: %s", randomStringPointer); 

do { 

    /* For loop to copy the first randomly generated string */ 
    for(i = 0; i < 42; i++) 
     *(randomStringPointer + i) = copyString[i]; 

    randomStringPointer = copyString; 

    i = 0; 
    lowercaseCheck = 0; 

    /* While loop to to get user input */ 
    printf("Please enter at least 2 capital letters and a maximum of 20.\n"); 
    while ((((*(userStringPointer + i)) = getchar()) != '\n')) { 

     /* Counter to determine how many characters were entered */ 
     i++; 

    } 

    /* Adding 1 to add to add null character */ 
    i++; 

    *(userStringPointer + i) = '\0'; 

    //printf("This is the user's string %s", userStringPointer); 

    /* Capital letter check */ 
    for (n = 0; n < 20; n++) { 
     if (((*(userStringPointer + n)) >= 'a') && (*(userStringPointer + n) <= 'z')) { 

      lowercaseCheck++; 
     } 
    } 

    if (--i < 3) { 
     printf("You need at least two letters\n"); 
    } 

    else if (i > 21){ 
     printf("You cannot have more than twenty letters\n"); 
    } 

    else if (lowercaseCheck == 0) { 

     puts(userStringPointer); 

     printf("Enter a character to replace occuring letters.\n"); 
     scanf("%c", &replacementCharacter); 
     getchar(); 

     //printf("this is the copy string before strFilter: %s", randomStringPointer); 
     //printf("This is the replacement character %c", replacementCharacter); 

     strFilter(randomStringPointer, userStringPointer, replacementCharacter); 

    } 

    else 

     printf("You must have 2 capital letters.\n"); 


    printf("Would you like to enter another string (y/n)?\n"); 
    loopContinue = getchar(); 
    getchar(); 


} while (loopContinue != 'n' && loopContinue != 'N'); 

} 

/* Function to replace letters with the character chosen by the user */ 
void strFilter(char * replacementCopyStringPointer, char * replacementUserStringPointer,   char c) 
{ 

int i = 0; 
int n = 0; 

while (n < 20) { 

    for (i = 0; i < 40; i++) { 
     if ((*(replacementCopyStringPointer + i)) == *(replacementUserStringPointer + n)){ 
      *(replacementCopyStringPointer + i) = c; 
     } 
    } 
    i = 0; 
    n++; 
} 

puts(replacementCopyStringPointer); 
} 
+0

'fillS2' prend un paramètre nommé' userStringPointer', mais il change immédiatement 'char *' 'où les points userStringPointer' à. Cela signifie qu'il est totalement inutile de le prendre comme paramètre. (Peut-être que vous attendez un comportement de passage par référence?) – ruakh

+0

Merci pour le commentaire. Cela a vraiment éclairci beaucoup pour moi. – user1681673

Répondre

1

Le tableau randomlyGeneratedArray dans fillS1 serait détruit une fois la fonction fillS1 renvoyée. Vous devez allouer de la mémoire à heap pour randomlyGeneratedArray.

randomlyGeneratedArray = (char *)malloc(sizeof(char)*41)

font également la même chose pour userStringPointer dans fillS2. Cela devrait résoudre le problème.

Pour la différence entre la pile et tas lu cette question What and where are the stack and heap?

+0

Merci. Je vais jouer avec cela en utilisant vos recommandations. – user1681673

Questions connexes