Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 103
AVI wait icons for CAnimateCtrl in Visual Studio 2008 MFC 1 119
Problem to ASCII 1 165
change colour of repeater control in asp.net c# 7 71
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

939 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

6 Experts available now in Live!

Get 1:1 Help Now