[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-26
3
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 1000 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 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

649 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