Solved

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

Posted on 2011-02-26
3
309 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

777 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