Solved

changing variable value by passing it to function parameter doesn't work??

Posted on 2011-02-26
3
306 Views
Last Modified: 2012-05-11
why wouldn't the following code pass the char to the deqmessage? as the output shows nothing

I must be missing something here, but I really thought that would work
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
void deq_msg(char);
char deqmessage;


void deq_msg(char msg)
{
msg = 'f';
}

void main()
{
deq_msg(deqmessage);
cout<<deqmessage;
}

Open in new window

0
Comment
Question by:logicallayer
3 Comments
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 250 total points
ID: 34989534
It has to be a pointer.

 
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
void deq_msg(char);
char deqmessage;


void deq_msg(char *msg)
{
*msg = 'f';
}

void main()
{
deq_msg(&deqmessage);
cout<<deqmessage;
}

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 34989540
That had never worked before and won't ever work in the future (the function will only receive a copy) - to change a value so that the change persists in teh calling funciton, you need to pass it by reference or by pointer, e.g.
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
void deq_msg(char);
char deqmessage;


void deq_msg(char& msg) // by reference
{
msg = 'f';
}

void deq_msg(char* msg) // by pointer
{
*msg = 'f';
}

void main()
{
deq_msg(deqmessage); // by reference
deq_msg(&deqmessage); // by pointer
cout<<deqmessage;
}

Open in new window

0
 

Author Comment

by:logicallayer
ID: 34989600
opps, I guess I'm too over whelmed with my assignment that I started pull stupid mistakes,

thanks guys,
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now