Returning a string

This is a discussion on Returning a string within the C++ Programming forums, part of the General Programming Boards category; I am trying to pass a string from one function to another and am going around in circles. PLEASE HELP!!! ...

  1. #1
    LAN
    LAN is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Returning a string

    I am trying to pass a string from one function to another and am going around in circles. PLEASE HELP!!!

    The following function is where I call the function

    void readRTC()
    {
    char RTC[6];
    RTC = Read_RTC(&RTC);
    cout<<RTC;
    }

    Calling the function below.

    ??????? Read_RTC(char* Buffer)

    {

    unsigned int i;
    unsigned char sec,ten_sec,time1,time1_ten,time2,time2_ten;
    char Buffer[6];


    cout<<"RTC Time";

    if(RTC_ready()) //check status of rtc
    {

    RaiseSS_RTC();
    SendByte(0x00); //send start address, this will auto inc
    //read addresses 0x00 - 0x10 this includes all time/date and alarm0&1 and regs
    for(i=0;i<17;i++)
    {

    RTC_RX_data[i]=SendByte(0x00); //send dummy byte to read from address
    }

    LowerSS_RTC();
    time2_ten =(RTC_RX_data[2]>>4)&0x0f;
    cout<<(unsigned int)(unsigned char) time2_ten;
    time2 =(RTC_RX_data[2])&0x0f;
    cout<<(unsigned int)(unsigned char) time2;
    time1_ten =(RTC_RX_data[1]>>4)&0x0f;
    cout<<(unsigned int)(unsigned char) time1_ten;
    time1 =(RTC_RX_data[1])&0x0f;
    cout<<(unsigned int)(unsigned char) time1;
    ten_sec=(RTC_RX_data[0]>>4)&0x0f;
    cout<<(unsigned int)(unsigned char) ten_sec;
    sec=(RTC_RX_data[0])&0x0f;
    cout<<(unsigned int)(unsigned char) sec;



    sprintf(Buffer, "%d%d"":""%d%d"":""%d%d",time2_ten,time2,time1_ten ,time1,ten_sec,sec);
    cout<<Buffer;


    return Buffer;

    }
    else
    {

    cout<<"RTC initialise failure"<<endl;
    return 0;
    }


    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    this has nothing to do with C!
    use std::string instead of char*

    or if you want to code C then you have the pass the return value as pointer TO the function:

    void doSomeThing(char* s);

    doSomeThing now may fill s with values! so the caller has to provide the memory...
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Consider the following simple program...

    Code:
    #include <stdio.h> 
    
    void abcd (char *);
    
    int main() 
    {
    	char a[6];
    	abcd(a);
    	printf("%s\n",a);
    	return 0;
    }
    
    void abcd(char *x)
    {
    	sprintf(x,"Hello");
    }
    ... also consider posting this on the C++ board!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I meant post C++ questions there, not crosspost it there!!! You posted a C++ question on the C board!

    There, I've moved this one and deleted your crosspost.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Unregistered
    Guest
    Here's a C++ version of passing the same string to several functions.

    #include <iostream.h>
    #include <string.h>

    void abcd (char *);
    void bar(char *);

    int main()
    {
    char a[20];
    abcd(a);
    cout << "string a ends up as : " << a;
    return 0;
    }

    void abcd(char *x)
    {
    strcpy(x, "hello ");
    cout << "string x starts as : " << x << endl;
    bar(x);
    cout << "string x now is " << x << endl;
    }

    void bar(char * y)
    {
    char foo[7] = "world";
    cout << foo << endl;
    strcpy(y, foo);
    cout << "string y is now " << y << endl;
    }

    x, y, and a should end up the same because they ARE all the same string being handed from function to function by reference using pointer syntax.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I once read of something called typename of something like that. I am not sure what it does but might be what is needed. Someone who knows please respond.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21