473,405 Members | 2,282 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,405 software developers and data experts.

Pointer trouble

Hi all,

I am facing a problem with a library that allocates memory internally.
My basic problem is how to reflect changes in pointer values back in
main.

My library routine allocates dynamically and returns char ** (array of
strings). To track changes in main I pass a char*** and dereference the
passed value in foo (this is unsafe, don't know what it's pointing to).

void foo (char ***i, char ***j)
{
/* i and j are allocated dynamically here */
**i = lib_routine();
....
**j = lib_routine();

}
/* In main, i want to know changes made to pointer values */
int main(void)
{
char ***a, char ***b;
foo(a, b)
/* Do something with *a[0] and *b[0] */
}

But isn't it unsafe (I dereference an unknown pointer when I write **i
= lib_routine();) ? How do I track changes in pointer values in a
called function ?

Thanks

PS - I wanting something on the lines of a reference to pointer in C++.

Nov 14 '05 #1
3 1360
void foo (char ***i, char ***j)
{
/* i and j are allocated dynamically here */
**i = lib_routine(); Should be *i = lib_routine();...
**j = lib_routine();

Should be *j = lib_routine();

}

Nov 14 '05 #2
On Fri, 17 Dec 2004 01:42:14 -0800, Kelvin Moss wrote:
Hi all,

I am facing a problem with a library that allocates memory internally.
My basic problem is how to reflect changes in pointer values back in
main.

My library routine allocates dynamically and returns char ** (array of
strings). To track changes in main I pass a char*** and dereference the
passed value in foo (this is unsafe, don't know what it's pointing to).
It is up to the caller to pass a pointer to something valid.
void foo (char ***i, char ***j)
{
/* i and j are allocated dynamically here */
**i = lib_routine(); ...
**j = lib_routine();
That should be *i and *j
}
/* In main, i want to know changes made to pointer values */
int main(void)
{
char ***a, char ***b;
foo(a, b)
But your caller here isn't passing a valid pointer, it is passing
uninitialised values which is always an error. What you need is

char **a, char **b;
foo(&a, &b);

which is passing valid addresses if char ** objects that foo can write to.
/* Do something with *a[0] and *b[0] */


You now can.

Lawrence
Nov 14 '05 #3
>char **a, char **b;
foo(&a, &b);
which is passing valid addresses if char ** objects that foo can write

to.

oh yes..thanks much. I guess too many pointers and lack of sleep
confused me :-)

Nov 14 '05 #4

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

Similar topics

2
by: Tom Alcendor | last post by:
I am building a chess game application that allows a user to click on a piece and move it to a new square. When the user clicks on a piece the mouse pointer must change to the image associated...
2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
16
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction,...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
18
by: Jacek Dziedzic | last post by:
Hi! I'm trying to squeeze a few clock cycles from a tight loop that profiling shows to be a bottleneck in my program. I'm at a point where the only thing that matters is execution speed, not...
8
by: subramanian100in | last post by:
Supoose we have, int x; int *p = &x; int *q; q = p + 1;
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
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
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.