Thread: casting a pointer as static

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    casting a pointer as static

    Hi,
    Why can't I get static to work here? Where static is highlighted in blue.
    Code:
    #include <stdio.h>
    
    main()
    
    {
          char source[] = "qazwsxedcrfvtgbyhnujmikolp";   
          char * src_ptr;                                    
        
          char target[] = "lkjhgfdsaqwertyuiopmnbvcxz";    
          char * tar_ptr;
        
          char temp[51];
          char * temp_ptr;
          
         
          int counter=0,
              i=0,
              t=0;
          
          printf("\nPlease enter a line of text: ");
          gets(temp); 
     
     for(temp_ptr = temp; *temp_ptr != '\0'; ++temp_ptr)
     ++t;
          
    while(i<t)
                
     {          
     
    
          for(src_ptr = source, static temp_ptr = temp; *temp_ptr && *src_ptr != '\0';)
          
             {
               if(*temp_ptr != *src_ptr)
               {
                 ++src_ptr;
                 continue;
               }
                  
               else
               {       
                 ++temp_ptr;    
                  break;                              
               } 
             }
               ++i;       
     }
     
     getchar();
    
    }
    Last edited by richdb; 04-18-2006 at 10:18 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps because C(89) does not allow inline declarations? Or perhaps because that symbol was previously decloared? What are you attempting to accompish?
    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
    Jan 2006
    Posts
    100
    Its a problem from a book, to do a simple encryption on a string. It basically looks at the character from the user, checks the 'source' array for that character, and replaces the user's character with the corresponding array character in the 'target' array. So if the first letter in the user's string is 'a', the program finds 'a' in the 'source' string, which is element 1, and would replace it with element 1 from the 'target' array, which is 'k'. It would repeat this for each element in the user entered string. I need the temp_ptr to retain its value instead of reinitializing when the for loop starts over. That way the program will move through each letter of the user input, searching the source string for each one, then going to the next one. (This is only the searching part of the program, I haven't moved on to displaying the encrypted string until I get this correct)

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I meant, "What is it that you expect adding the keyword static to do for you?"

    Have you broken things into separate functions yet?

    Have you seen the FAQ? (Obviously not, because there you would learn never to use ]gets.)
    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
    Jan 2006
    Posts
    100
    I want to use static to force the pointer, (only the for loop instance of it) to retain its value instead of being reinitialized when the loop starts over.

    I know about gets but I'm trying to learn primarily from one book and it hasn't gone into fgets yet.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Bottom line, you can't do what you are trying like that. Perhaps reset the pointer.

    I'm calling it a night -- 20 questions is out for now. I see what you are saying, but using a separate function makes life so much easier.

    This is what I would do (in part).
    Code:
    char encrypt_char(char src)
    {
       static const char source[] = "qazwsxedcrfvtgbyhnujmikolp";   
       static const char target[] = "lkjhgfdsaqwertyuiopmnbvcxz";    
       size_t i;
       for ( i = 0; source[i] != '\0'; ++i )
       {
          if ( src == source[i] )
          {
             return target[i];
          }
       }
       return src; /* someting else */
    }
    Put that in a simple loop...
    Code:
    void encrypt_string(const char *src)
    {
       for ( ; *src; ++src )
       {
          putchar(encrypt_char(*src));
       }
    }
    Test it with user input or a canned string.

    Such things can be implemented inline in main as well.
    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.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Dave,
    I follow you completely, but let me make sure of some things. src is the user input? And why return src when the for loop ends?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    src is the character to be converted. if the conversion is not possible (not found) it simply returns the original character.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    But only once, when the for loop ends (hits '\0')
    Code:
    for ( i = 0; source[i] != '\0'; ++i )
       {
          if ( src == source[i] )
          {
             return target[i];
          }
       }
       return src; /* someting else */
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    right. if the code get's that far then the character wasn't found in the lookup table so it's just returned to the caller.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  3. casting - pointer to pointer
    By terminator in forum C Programming
    Replies: 56
    Last Post: 04-23-2008, 12:54 AM
  4. Pointer casting?
    By cpjust in forum C Programming
    Replies: 8
    Last Post: 11-09-2007, 09:55 AM
  5. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM