Thread: How to return a char * value from a function?

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    7

    How to return a char * value from a function?

    Hi I'm having trouble returning an 8 char string from a function. I can get the correct value displayed from printf("%c%c%c%c%c%c%c%c \n", bit0 ,bit1 ,bit2 , bit3, bit4, bit5, bit6, bit7); but can't get into a char * to return the string to the calling function. Any help would be greatly appreciated.

    My code is below:
    Code:
    char * GetPassword(void){
      char c;
      char * password;
      char bit0 = '0';
      char bit1 = '0';
      char bit2 = '0';
      char bit3 = '0';
      char bit4 = '0';
      char bit5 = '0';
      char bit6 = '0';
      char bit7 = '0';
    
    
        PORTJ |= (1<<DDJ0)|(1<<DDJ1); // Enables the internal pullups for PORTJ0 and PORTJ1
        PORTH |= (1<<DDH0)|(1<<DDH1); // Enables the internal pullups for PORTH0 and PORTH
        PORTD |= (1<<DDJ0)|(1<<DDJ1)|(1<<DDJ2)|(1<<DDJ3); // Enables the internal pullups for PORTJ0, PORTJ1, PORTJ2 and PORTJ3
    
    
        printf("Please Enter Your Password  \n");
    
    
        if ((~PINJ & (0<<PJ1)))
        {
            bit0 = '0';
        }
        else if ((~PINJ & (1<<PJ1)))
        {
          bit0 = '1';
        }
        if ((~PINJ & (0<<PJ0)))
        {
            bit1 = '0';
        }
        else if ((~PINJ & (1<<PJ0)))
        {
            bit1 = '1';
        }
        if ((~PINH & (0<<PH1)))
        {
            bit2 = '0';
        }
        else if ((~PINH & (1<<PH1)))
        {
            bit2 = '1';
        }
        if ((~PINH & (0<<PH0)))
        {
            bit3 = '0';
        }
        else if ((~PINH & (1<<PH0)))
        {
            bit3 = '1';
        }
        if ((~PIND & (0<<PD3)))
        {
            bit4 = '0';
        }
        else if ((~PIND & (1<<PD3)))
        {
            bit4 = '1';
        }
        if ((~PIND & (0<<PD2)))
        {
            bit5 = '0';
        }
        else if ((~PIND & (1<<PD2)))
        {
            bit5 = '1';
        }
        if ((~PIND & (0<<PD1)))
        {
            bit6 = '0';
        }
        else if ((~PIND & (1<<PD1)))
        {
            bit6 = '1';
        }
        if ((~PIND & (0<<PD0)))
        {
            bit7 = '0';
        }
        else if ((~PIND & (1<<PD0)))
        {
            bit7 = '1';
        }  
    
    
        c = getchar();
        while (getchar() != '\n');
        printf("%c%c%c%c%c%c%c%c \n", bit0 ,bit1 ,bit2 , bit3, bit4, bit5, bit6, bit7);
    
    
        *(password+1) = bit0;
        *(password+2) = bit1;
        *(password+3) = bit2;
        *(password+4) = bit3;
        *(password+5) = bit4;
        *(password+6) = bit5;
        *(password+7) = bit6;
        *(password+8) = bit7;
    
    
        return password;    
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    There's a few things wrong with your code.
    * password has no allocated space. It's just an uninitialized pointer.
    * 0 << x doesn't make sense. It's just 0.

    It's usually best for the caller to pass in an allocated string for the function to fill.
    Maybe try something like this.
    Code:
    void GetPassword(char *pass) {
        PORTJ |= 1<<DDJ0 | 1<<DDJ1; // Enables the internal pullups for PORTJ0 and PORTJ1
        PORTH |= 1<<DDH0 | 1<<DDH1; // Enables the internal pullups for PORTH0 and PORTH
    
    //// Are these correct? You've already enabled them for PORTJ0 and PORTJ1 above.
    //// Should they maybe be DDD0, DDD1, etc? (I have no idea.)
        PORTD |= 1<<DDJ0 | 1<<DDJ1 | 1<<DDJ2 | 1<<DDJ3; // Enables the internal pullups for PORTJ0, PORTJ1, PORTJ2 and PORTJ3
     
        printf("Enter your password\n");
    
        pass[0] = (~PINJ & 1<<PJ1) ? '1' : '0';
        pass[1] = (~PINJ & 1<<PJ0) ? '1' : '0';
        pass[2] = (~PINH & 1<<PH1) ? '1' : '0';
        pass[3] = (~PINH & 1<<PH0) ? '1' : '0';
        pass[4] = (~PIND & 1<<PD3) ? '1' : '0';
        pass[5] = (~PIND & 1<<PD2) ? '1' : '0';
        pass[6] = (~PIND & 1<<PD1) ? '1' : '0';
        pass[7] = (~PIND & 1<<PD0) ? '1' : '0';
        pass[8] = '\0'; // null-terminate the string (if you want)
    }
    
    void caller() {
        char password[9];  // must have at least 9 chars
        GetPassword(password);
        printf("%s\n", password);
    }
    I have no idea if PINJ, etc, should be bitwise-complemented, like ~PINJ, as you are doing. So if it doesn't work correctly you might try it without that.
    Last edited by john.c; 08-15-2018 at 12:03 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    7
    Thank you, that helped a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can I write a function to return char * [5]
    By tester1234 in forum C Programming
    Replies: 17
    Last Post: 12-16-2015, 02:01 AM
  2. how to create a function return with char* array
    By tester1234 in forum C Programming
    Replies: 7
    Last Post: 12-09-2015, 11:25 AM
  3. How to Return Char Array in function?
    By homoon in forum C Programming
    Replies: 3
    Last Post: 04-20-2012, 03:12 AM
  4. Can a function return char variable?
    By mystic in forum C Programming
    Replies: 10
    Last Post: 07-13-2010, 01:05 AM
  5. getting a function to return a char array
    By variable in forum C Programming
    Replies: 20
    Last Post: 02-10-2005, 08:13 PM

Tags for this Thread