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

pass pointer by reference

Folks,

I want to write a init string function that mallocs an area, fills it
with a char, sticks a NUL char in the last position and returns it to
the user.

So far I noticed that my implementation always works over a NEW memory
area, and not over the ORIGINAL pointer area, so the initial string
area is untouched, the pointer is not update to refer to the new
initiated area...

What is going on? See my code below...

int main(){
char *string;
initstring(string, '\0', 14); /* create a string filled with NUL
and a extra NUL at position 15 */
/* if I check *string here, it is untouched since the 1st statement
*/
}

void initstring(char *array, char c, unsigned int size){
array = malloc((size + 1)* sizeof(char));
int i;

for(i=0;i<size;i++){
/* array[i] = c; array indexing */
*array = c;
array++;
}
/* array[size+1] = '\0'; array indexing */
*array = '\0';
}

So, why "*array" always get into the function initstring with a
diferent value (memory address) than when the initistring() function
is called (according)?

My environment: Eclipse + CDT + Cygwin (Win32)

Sep 12 '07 #1
11 8392
HSeganfredo wrote:

[...]
int main(){
char *string;
initstring(string, '\0', 14); /* create a string filled with NUL
and a extra NUL at position 15 */
/* if I check *string here, it is untouched since the 1st statement
*/
}
[...]
void initstring(char *array, char c, unsigned int size){
array = malloc((size + 1)* sizeof(char));
You pass a *copy* of the pointer to the initstring function.
The modification of array in initstring won't affect the string variable
in main.

You must pass a pointer to pointer, if you want to modify the pointer.

initstring(&string, '\0', 14);
/* ... */
void initstring(char **parray, char c, unsigned int size){
char* array;
array=*parray = malloc((size + 1)* sizeof(char));

BTW, you should respect the sixth commandment for the C programmer:
"
If a function be advertised to return an error code in the event of
difficulties, thou shalt check for that code, yea, even though the checks
triple the size of thy code and produce aches in thy typing fingers, for
if thou thinkest ``it cannot happen to me'', the gods shall surely punish
thee for thy arrogance.
"

You must check the return value of malloc()!

--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 12 '07 #2
initstring(string, '\0', 14);

As someone has already said, you have to pass the address of the
pointer, i.e. a pointer to a pointer.

Really though, I'd just return a pointer from the function:

Unchecked, untested code. . .

char *const initstring(char const c,size_t const len)
{
char *const retval = malloc(len + 1);

char *p = retval;
char const *const pnull = retval + len;

while (p != pnull) *p++ = c;

*p = 0;

return retval;
}
I use the const in the return value as an indicator to the programmer
that they might want to hang on to the value.

Martin

Sep 12 '07 #3
Thanks for the "pointers", worked here perfectly.

I started to remember that a pointer reference "func(&ptr)" in the
calling function always implies on a double pointer in the prototype
(**ptr), one contains the original pointer reference (to char in my
case) and another one containing the reference to that pointer. It´s a
somewhat complex subject that makes years I don´t deal with. Thanks.

Sep 12 '07 #4
HS*********@gmail.com wrote:
void initstring(char *array, char c, unsigned int size){
array =
Whenever you see a function definition,
where a parameter is being overwritten
without being read first, that's a bad sign.

--
pete
Sep 12 '07 #5
Martin wrote:
) char *const initstring(char const c,size_t const len)
) {
) char *const retval = malloc(len + 1);
)
) char *p = retval;
) char const *const pnull = retval + len;
)
) while (p != pnull) *p++ = c;
)
) *p = 0;
)
) return retval;
) }

I find that a very convoluted way to write:

if (retval = malloc(len + 1)) {
memset(retval, c, len);
retval[c] = 0;
}
return retval;
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Sep 13 '07 #6
Willem wrote:
Martin wrote:
) char *const initstring(char const c,size_t const len)
) {
) char *const retval = malloc(len + 1);
)
) char *p = retval;
) char const *const pnull = retval + len;
)
) while (p != pnull) *p++ = c;
)
) *p = 0;
)
) return retval;
) }

I find that a very convoluted way to write:

if (retval = malloc(len + 1)) {
memset(retval, c, len);
retval[c] = 0;
You probably meant retval[len] = 0;
}
return retval;
--
Denis Kasak
Sep 13 '07 #7
Denis wrote:
) Willem wrote:
) if (retval = malloc(len + 1)) {
) memset(retval, c, len);
) retval[c] = 0;
)
) You probably meant retval[len] = 0;
)
) }
) return retval;

It's a well-known Law of the Useniverse that any post which is made to
correct/improve upon any piece of language must contain a silly error.

I guess C counts as a language as well for that Law. :-)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Sep 13 '07 #8
Thanks everybody!

Sep 13 '07 #9
Willem:
I find that a very convoluted way to write:

if (retval = malloc(len + 1)) {
memset(retval, c, len);
retval[c] = 0;
}
return retval;

You're right I shuda used memset... it slipped my mind that there's a
Standard Library function for doing that.

Martin

Sep 13 '07 #10
On Sep 12, 6:24 pm, "André Gillibert"
<tabkanDELETETHIS...@yahodeletethato.frwrote:
>
BTW, you should respect the sixth commandment for the C programmer:
Salut André,

Ou est ce que les commandments de C?

Lisp 9000

Sep 13 '07 #11
li******@gmail.com wrote:
Ou est ce que les commandments de C?
You can read the ten commandments for the C programmer at:
http://www.lysator.liu.se/c/ten-commandments.html

Google helps:
http://www.google.com/search?q=ten+c...e+C+programmer
--
You can contact me at <ta*****************@yahoDELETETHATo.fr>
Sep 21 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
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...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
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...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
4
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is within the addStudent() function. I am able to...
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...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.