#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    28
    Rep Power
    0

    Called object type 'int' is not a function or function pointer


    I wrote this program to caesar encrypt a string (alphabetic characters only). (i.e. take all the characters in a sentence and rotate them key times - i.e. whatever number is entered as the key command line parameter)
    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char caesar_encode (char, int);  // prototype of caesar_encode function
    
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    int main (int argc, string argv[])
    {
        int key = 0;
        int length, base, asciiCode;
        string sentence;
        char currentLetter, caesar_encode;
    
        // if wrong number of command line arguments, shout at user then quit
        if (argc != 2)
        {
            printf("Incorrect number of command line arguments.\n\n");
            return 1;
        }
    
        // if key parameter is too low, shout at user then quit
        key = atoi(argv[1]);
        // printf("key = %i\n\n", key);
        if (key < 1)
        {
            printf("Key parameter is too low.\n\n");
            return 1;
        }
        
        // since key values modulo 26 are equivalent for the purposes of our program
        // if a key value is larger than 26, reduce it to its modulo 26 equivalent
        // (i.e. a number from 0 to 26)
        if (key > 26)
        {
            // printf("Reducing key larger than 26 to its modulo 26 equivalent.\n");
            // printf("Old key was %i\n.", key);
            key = key % 26;
            // printf("New key is %i.\n\n", key);
            
        }
        
        // prompt user for a string
        // printf("Enter a string to encrypt: ");
        sentence = GetString();
        string newSentence = sentence; // set up newSentence same length as sentence
        length = strlen(sentence);
        
        // loop through string, adjusting each letter according to the key value
        for (int i = 0; i < length; ++i)
        {
            printf("\n");
            // get current letter from sentence
            currentLetter = (char) sentence[i];
            
            // only Caesar encrypt currentLetter is it's actually a letter
            // (as per the pset2 spec, non-alphabetic characters aren't encoded)
            if (isalpha(currentLetter))
            {
            	newSentence[i] = caesar_encode (currentLetter, key);
    		}
        }
        printf("%s\n", newSentence);
        // printf("\n\n");
        return 0;
    }
    
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    // caesar encoding function - accepts a char and a key value as parameters
    // returns the caesar cipher encoded char
    // precondition: the function is only called if it has already been determined
    // that the char letter parameter is alphabetic. Therefore we don't need to test
    // for this inside the function.
    
    char caesar_encode (char letter, int key)
    {
    	char encoded_letter;
    	int base;
    	
    	// CODE TO CAESAR CIPHER ENCODE THE LETTER HERE
    	// if lowercase letter, base = 'a' - 1;
    	// if uppercase letter, base = 'A' - 1;
        // and currentLetter = base + ((currentLetter + key) % 26)
    	if (letter >= 'a' && letter <= 'z')
    		base = 'a' - 1;
    	if (letter >= 'A' && letter <= 'Z')
    		base = 'A' - 1;
    	printf("base = %i\n", base);
        
        printf("Before encoding, letter was: %c", letter);
        encoded_letter = (char) (base + (int)((int) letter + key));	// encode letter
    	printf("After encoding, letter is: %c", encoded_letter);
    	
    	return encoded_letter;
    }
    
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    I'm getting the following error, however:

    caesar.c:63:48: error: called object type 'int' is not a function or function pointer
    newSentence[i] = (char) caesar_encode ((char) currentLetter, key);
    ~~~~~~~~~~~~~ ^

    Can anybody help? Thanks! :/
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    newSentence[i] = (char) caesar_encode ((char) currentLetter, key);

    You are using caesar_encode as a function which takes two arguments. You even have a function prototype and a function implementation by that name. However:
    Code:
    char caesar_encode (char, int);  // prototype of caesar_encode function
    
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    int main (int argc, string argv[])
    {
        int key = 0;
        int length, base, asciiCode;
        string sentence;
        char currentLetter, caesar_encode;
    Within main, from where you call that function, you reused the name to be a char variable, which is an integer type. That reuse of the name hides the function. When an identifier in the local scope has the same name as an identifier outside that scope, it's the one within the local scope that is used. Scope and name hiding are important concepts in C, C++, and a number of other languages.

    Change the name of either the function or of the variable. Changing the function to CaesarEncode would be my choice.
    Last edited by dwise1_aol; February 12th, 2013 at 06:16 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    28
    Rep Power
    0
    Thanks dwise!

    This was a problem. Another problem was my not deducting the base from (char value + key) part of the calculation to encode the char. Spent a week scratching my head over that problem set! Just glad I finally figured it out! :D

    Once again, thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo