473,379 Members | 1,222 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,379 software developers and data experts.

Simple pass-by-reference question

42
How do I get the pass by reference-thing to work in this example?
Since I'm still new to C I sometimes get confused in this matter, and this is one of those occasions.

I'm obviously doing something wrong.
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3. char *params =  mem_alloc(sizeof(char)*64);
  4. getParams(params);
  5. printf("Parameters: %s",params);
  6. mem_free(params);
  7. }
  8.  
  9. int getParams(char *p)
  10. {
  11.   p = "something, something";
  12.   return 0;
  13. }
what I want is of course the result to be "Parameters: something, something".
Thanks!
Jul 3 '08 #1
3 1279
JosAH
11,448 Expert 8TB
There doesn't exist a call by reference parameter passing mechanism in C; it only
uses call by value for passing parameters around. You can still copy your string
to that chunk of memory. Change your line 11 as follows:

Expand|Select|Wrap|Line Numbers
  1. strcpy(p, "something, something");
  2.  
kind regards,

Jos
Jul 3 '08 #2
MimiMi
42
Ok, thank you very much, my problem is solved!
However, I'm trying to really understand why just writing
Expand|Select|Wrap|Line Numbers
  1. p="something,something";
  2.  
doesn't work. While debugging, I see that the params-pointer and the p-pointer both point to the same address. That's why I'm confused.
But is it because the pointer p only gets assigned to point to the string 'locally', so once we're outside the scope of the getParam - method, this assignment is all forgotten about? If that's the case, I think I understand. Anyhow, again thank you so very much!
Jul 3 '08 #3
JosAH
11,448 Expert 8TB
But is it because the pointer p only gets assigned to point to the string 'locally', so once we're outside the scope of the getParam - method, this assignment is all forgotten about? If that's the case, I think I understand. Anyhow, again thank you so very much!
Yep, that's how call by value works: just values are passed to a function and
assigned to the parameters as if they were local variables. If you change them
you don't change anything outside that function. If such a value happens to be
an address and you change some memory content at that address, the changes
will be permanent then, i.e. not local to that function.

kind regards,

Jos
Jul 3 '08 #4

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

Similar topics

2
by: will taubin | last post by:
i would like my program to have a config.php with passwords and other stuff. i would like to have a functions.php to hold all my functions. i would like the functions.php to include/require...
4
by: Steven | last post by:
Hi there, I want to pass a session id from a form to a third party payment system. I have no need a of creating and maintaining sessions for the whole visit to the site. Do I just start the...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
3
by: ben | last post by:
All, What I have: I have written a VB.net application that uses Access DBs and Make 100s of Excel documents from the data. What I need: I need a Simple ASP.NET Page that will basically pass...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
0
by: RSH | last post by:
Hi, I am having to temporarily work over an existing website. I have a template include page that I would like to pass a string variable that will change based on the parent that calls the...
1
by: Jerry | last post by:
I know this is too easy, but I am not getting it. I want to regex string to find any string that has non numeric characters, integers and floats verification basically. I am using the...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
7
by: bvdp | last post by:
Is there a simple/safe expression evaluator I can use in a python program. I just want to pass along a string in the form "1 + 44 / 3" or perhaps "1 + (-4.3*5)" and get a numeric result. I can...
3
by: nembo kid | last post by:
I have an issue with a simple function that has to make a linear search for a key into an array. If the key is found in the array, the function it has to return 1 to the caller and pass array...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.