Thread: Trying to send string to a function and return it back.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Post Trying to send string to a function and return it back.

    Not sure what i am doing wrong, I need to send "abc" to function and decrypt it by adding 3.

    Code:
    #include <stdio.h>
    #include <string.h>
    char* shift(char *word, int key);
    int main()
    {
        char word[3]="abc";
    
    
        printf("\nThe key move is: %s\n", shift(word, 3));             
        return 0;
    }
    
    
    char *shift(char *word, int key)
    {
        int i;
        int offset;
        char decryptedLetter;               
        char decryptedword; 
    
    
        for (i=0;i<strlen(word);i++)
        {
           offset = 'word[i]'- 'A';
           offset = (offset + key) % 26;
           decryptedLetter = 'A' + offset;
           char decryptedword=decryptedLetter;
        }
        return decryptedword;
    }
    ERRORS/Warnings
    text.c: In function âshiftâ:
    text.c:20:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    text.c:22:17: warning: character constant too long for its type [enabled by default]
    text.c:25:13: warning: unused variable âdecryptedwordâ [-Wunused-variable]
    text.c:27:5: warning: return makes pointer from integer without a cast [enabled by default]
    text.c:27:5: warning: âdecryptedwordâ is used uninitialized in this function [-Wuninitialized]


    >a.out

    Segmentation fault
    Last edited by shank09; 04-06-2013 at 09:55 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    One of the problems is that decryptedword is a single character and you are trying to return a char pointer. Second I'm not sure what your trying to do with this line:
    Code:
    offset = 'word[i]'- 'A';
    But surrounding word[i] with single quotes is incorrect for any use. Single quotes signify a character constant, and character constants can only have one char.


    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Ok, i updated my code. no errors but warnings. and not getting the right result also.

    Code:
    #include <stdio.h>
    #include <string.h>
    char* shift(char *word, int key);
    int main()
    {
        char word[3]="abc";
    
    
        printf("\nThe key move is: %s\n", shift(word, 3));
        return 0;
    }
    
    
    char *shift(char *word, int key)
    {
        int i,n;
        int offset;
        char decryptedLetter;
        char decryptedword [3]= {};
        for (i=0;i<strlen(word);i++)
        {
           offset = word[i]- 'A';  
           offset = (offset + key) % 26;
           decryptedLetter = 'A' + offset;
           for (n=0; n<=i; n++)
           {
              decryptedword[n]=decryptedLetter;
           }
        }
        return decryptedword;
    }
    Warnings
    text.c: In function âshiftâ:
    text.c:19:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    text.c:29:5: warning: function returns address of local variable [enabled by default]

    >a.out

    The key move is: P~
    ·¿ân·àÄ·à
    d ô¿·>
    à
    @ Km·Ã·ô¿·Lm·
    ôabc




    I have no idea what happend

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Any one>? please...

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    text.c:29:5: warning: function returns address of local variable [enabled by default]
    You can't return a local array from your function. I recommend you use the parameter, change the values of the characters and then return that string.

    Also this line doesn't allow enough characters for the string:
    Code:
    char word[3]="abc";
    You need to leave room for the end of string character.

    Jim

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by shank09 View Post
    Any one>? please...
    Strings in C don't really exist. What you get is a pointer to a memory buffer, which contains characters.
    So you can't say "return a string" without specifying what you mean. The two most common approaches are to return a buffer allocated with malloc, or to fill in a buffer passed by caller.
    If the input is always the same length or shorter than the output, sometimes it makes sense to modify the string in place, using the same buffer for input and output. However if you do this, you have to be careful not to pass a const string, which calling with a literal will do.

    Look up strcpy, which uses the passed buffer approach, and strdup, which does the same thing, but returning a malloced buffer.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    I think when you call your function and it returns the pointer it is going out of scope because the string is only declared locally in the function. so here is what i got


    Code:
    PHP Code:
    #include "stdafx.h" #include <stdio.h> #include <string.h> charshift(char *wordint key);     char decryptedword [4]= {}; int main() {     char word[4]="abc";         printf("\nThe key move is: %s\n"shift(word3));     char c getchar();     return 0; }     char *shift(char *wordint key) {     int i,n;     int offset;     char decryptedLetter;     for (i=0;i<strlen(word);i++)     {        offset word[i]- 'A';          offset = (offset key) % 26;        decryptedLetter 'A' offset;        for (n=0n<=in++)        {           decryptedword[n]=decryptedLetter;        }     }     decryptedword[strlen(word)] = '\0';     return decryptedword; } 

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please avoid the use of a global variable, this is probably not what your instructor is trying to teach you.

    Jim

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    ya just add another parameter in your function so u can pass the address to the local scope of your function your calling and than write the data to a buffer.

    like this for example.
    Code:
    char *shift(char *source, int key, char * destination)

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Post

    Thanks for the Help!

    I am able to do it now.

    My next task, is to calculate "letter frequencies" from an array.
    ok, I have two arrays, one with words and the other with the letter frequencies.
    the words array contains the ciphertext lets say "QXPQB", the letter frequencies array contains freqs. 0.08167, 0.01492... and so on.

    basically the array letter frequencies has 26 freqs, each one corresponds to the english letter.

    A is 0.08167, B is 0.01492, C is...

    Here is what i need to do.
    If ciphertext = QXPQB:


    Shift of 1 gives RYQRC.


    Calculate (freq of R) x (freq of Y) x (freq of Q) x (freq of R) x (freq of C)
    = 0.05987 x 0.01974 x 0.00095 x 0.05987 x 0.02782
    = 1.9 x 10-9
    How would i start this. should I
    Code:
    // The sample string is "ABC"
    
    int A=0, int B=1, int C=2, int D=3, int E=4; // and so on
    // Then maybe text[1]=B so B will int 1^in the above declaration letterfreq[B]=0.01492
    
    freqValue = letterfreq[text[1]] ;
    Will that work, or is there an easier way?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a function to return a string
    By uural4792 in forum C Programming
    Replies: 18
    Last Post: 11-01-2011, 04:51 AM
  2. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 08:10 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM