[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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

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
logicallayer
Asked:
logicallayer
2 Solutions
 
sergiobg57Commented:
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
 
jkrCommented:
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
 
logicallayerAuthor Commented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now