473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pass a variable memory address to a function and assign a value from within.

How to Pass a memory address(of a string variable) to function B and assign a value(a string) from within the function B.

Here's part of the program:

#include <stdio.h>
char sales[20];
int d;
main()
{
FunctionA(&sales);
printf("%s",sales);
}


FunctionB(int a, int *b)
{
char c[]="Hello World";
a=d;

*b=c; /* This is where I am confused */
rerturn NULL;
}

I would appreciate any help.
Thanks in advance.

Cheers.
Jan 28 '07 #1
1 2558
horace1
1,510 Expert 1GB
you cannot return the contents of c[] by setting pointer *b to it
Expand|Select|Wrap|Line Numbers
  1. FunctionB(int a, int *b)
  2. {
  3. char c[]="Hello World";
  4. a=d;
  5.  
  6. *b=c; /* This is where I am confused */
  7. rerturn NULL;
  8. }
  9.  
c[] is a local variable and the contents will be lost when the function exits and b will end up pointing at grabage

you need to copy c[] to b[], e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // pass pointer to char[] to receive data
  5. void FunctionB(char *b)
  6. {
  7. char c[]="Hello World";
  8. strcpy(b, c);    // copy contents of c[] to b[]
  9. }
  10.  
  11. int main()
  12. {
  13.     char x[20];  // must have sufficent room to receive characters
  14.     FunctionB(x);
  15.     cout << x << endl;
  16.     cin.get();
  17. }
  18.  
(1) in the calling function you need an array of sufficent size to receive the characters
(2) you pass a pointer to this array to the function
(3) use strcpy() to copy the contents of the local array to the array in the calling function
Jan 28 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
14
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual techinical stuff.....i have had a lot of hot debate...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.