Hello !

I'm writing a program in C++ and I use a struct definition named Contact.
In a function in my program that returns an item of type Contact,i face the problem below:

Contact getContact(){

    Contact c;
    //do things
    if(condition)
        //assign c
     else
        //c is not assigned
     return c;
}

So when it goes in else statement,the c remains unassigned,and the return c gives an error of course.
Is there a way to assign it with something like null so the return statement wouldn't face a problem?
Contact is just a simple struct.

You can't return null unless you are returning a pointer from a function. But if you don't want to use pointers there are a couple of options.
You could do something like this:

Contact getContact(){
    Contact c;
    // initialise c to some default state which indicates it's uninitialised
    // do some things
    if(condition)
        // assign c

    return c; // Returns default Contact if condition failed
}

That way when your function returns; if the returned contact is in it's default state you know the function failed.

Otherwise you could do something like this:

bool getContact(Contact &c){
    // do some things
    if(condition)
    {
        // assign values to c
        return true; // success
    }   
    return false; // failed
}

Then in your main program you can call it like this:

Contact c;
if(getContact(c))
{
    // getContact returned true, so c was initialised with some data
    // do whatever you need to do next
}
else // failed
{
    // Do whatever you need to do in event of failure
}

If you wanted to use a pointer, you could do this:

Contact* getContact(){
    Contact* c = 0; // initialise Contact ptr to null

    // do some things
    if(condition)
    {
        c = new Contact;
        // assign values to the struct pointed to by the pointer 
    }

    return c; // Returns null if condition failed
}

Then in your main program (or other wherever the function is called) you'd be able to check the returned pointer. If the returned pointer is null, you know the call to the function failed.
Also, because the above snippet uses the 'new' keyword, in the cases where the function returns a pointer to a valid object, you need to ensure you call delete on the pointer as soon as the returned object is finished with. Otherwise you'll have a memory leak!

thanks a lot!
and in the funtion getContact,when I assign c I simply do:
c=//assignment
right?
No need for pointers here?
(I did the 2nd solution with Contact &c)

The 2nd version passes a reference to an instance of Contact into the function, so you'd assign values like this:

c.member_value1 = value1;
c.member_value2 = value2;

etc.

yes of course but if inside the fuction I declare an item Contact c2 then it is possible to say:

c=c2;

right?

Yes you certainly could do that.
I don't know exactly what you're doing in your program, but from your description; when you mentioned c=//desription I imagined you were doing something like this:

bool getContact(Contact &c){
    // do some things...
    if(condition)
    {
        Contact c2;
        c2.foo = foo;
        c2.bar = bar;
        c = c2;
        return true; // success
    }
    return false; // failed
}

If this is the case, it would be more efficient to manipulate the passed in struct directly.
e.g.:

bool getContact(Contact &c){
    // do some things...
    if(condition)
    {
        c.foo = foo;
        c.bar = bar;
        return true; // success
    }
    return false; // failed
}

But again, it depends on exactly what you're doing in your code.

You really really helped me a lot!!
Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.