Thread: A problem with a pointer to a string

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

    A problem with a pointer to a string

    Hi. i was bored so i wrote a very simple encryption program using a global variable 'text' (the text to be encrypted) which i pass into two functions, encrypt and decrypt. This works fine, but as i've been told before that global variables are bad (i'm unsure why) i want to use a pointer to text instead of the global variable. However i don't have a very good understanding of pointers at all, so both my attempts to implement the code below with pointers failed miserably. Any help regarding this would be greatly appreciated.

    Thanks

    My code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void encrypt(char[]);
    void decrypt(char[]);
    
    char text[100];
        
    int main(void)
    {     
       char decision[10];
        
       printf("Enter text to be encrypted:");
       fgets(text, sizeof(text), stdin);
       text[strlen(text)-1] = '\0';
        
       encrypt(text);
       printf("%s\n", text);
    	
       printf("Do you want to decrypt the string?(yes/no)\n");
       fgets(decision, sizeof(decision), stdin);
       decision[strlen(decision)-1] = '\0';
    	
       if (strcmp(decision, "yes") == 0)
       {
          decrypt(text);
          printf("%s\n", text);
       }
       return 0;
    }
    
    void encrypt(char txt[])
    {
        int i;
        
        for (i = 0; i < strlen(txt); i++)
        {
            txt[i]++;
        }
    }
    
    void decrypt(char txt[])
    {
        int i;
        
        for (i = 0; i < strlen(txt); i++)
        {
            txt[i]--;
        }
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My test:
    Enter text to be encrypted:hello world
    ifmmp!xpsme
    Do you want to decrypt the string?(yes/no)
    yes
    hello world
    What's the question? If you just want text local to main, do so.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    my understanding was that if the text variable was not global, but local, i would be able to pass it into my functions, but i would not be able to change the variable from the function itself, unless i used a pointer to the variable. sorry if it was unclear but my question was simply how would i go about implementing the code i had already written (above), with a pointer to the variable 'text' (where text would be inside main()) instead of using 'text' as a global variable. Hope this makes sence.

    Thanks

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I may me a little off at the moment, but this does exactly the same as when text is global.
    Code:
    int main(void)
    {     
       char text[100];
       char decision[10];
        
       printf("Enter text to be encrypted:");
       fgets(text, sizeof(text), stdin);
       text[strlen(text)-1] = '\0';
        
       encrypt(text);
       printf("%s\n", text);
    	
       printf("Do you want to decrypt the string?(yes/no)\n");
       fgets(decision, sizeof(decision), stdin);
       decision[strlen(decision)-1] = '\0';
    	
       if (strcmp(decision, "yes") == 0)
       {
          decrypt(text);
          printf("%s\n", text);
       }
       return 0;
    }
    unless i used a pointer to the variable
    These are passing pointers.
    Code:
    void encrypt(char[]);
    void decrypt(char[]);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thanks i did not realise this.
    Last edited by edd1986; 03-20-2005 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 10:11 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM