473,698 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does one code something like this?


Hello,

I have a peice of code that I'm making an attempt to code. The problem
is that I need to return an arbitrary number of char strings.

int function(char *fname, int *dcount, char *data[])

Would this work? If so, then how to you load the data into data? Malloc?
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 2 '06 #1
7 1650
Hi,

I guess data should be declared like this Char ** data,
yes, malloc will do it...

Regards,

Jan 2 '06 #2
Daniel Rudy a écrit :
I have a peice of code that I'm making an attempt to code. The problem
is that I need to return an arbitrary number of char strings.

int function(char *fname, int *dcount, char *data[])

Would this work?


Nope.

char **function(char const *fname, int *dcount)

or better

struct data
{
size_t count;
char **arr;
};

int function (char const *fname, struct data *p_data)

--
A+

Emmanuel Delahaye
Jan 2 '06 #3

"Daniel Rudy" <sp******@spamt his.net> wrote
I have a peice of code that I'm making an attempt to code. The problem
is that I need to return an arbitrary number of char strings.

int function(char *fname, int *dcount, char *data[])

Would this work? If so, then how to you load the data into data? Malloc?

You can't do it that way.
The reason is that you don't know how many strings you have. Therefore you
need to call

char **ptr;
int N; / *set to number of strings */
ptr = malloc(N * sizeof(char *));

(in fact you would probably malloc() 1 and then call realloc() as you add
strings to the list, but that's just a detail).

The pointer therefore changes, so you can't pass it it. You need a char ***,
if you want to return it as a parameter.
A triple pointer is getting messy, so it's probably better to return the
pointer

char **function(char *fname, int *dcount)
Jan 2 '06 #4
At about the time of 1/2/2006 2:26 PM, Malcolm stated the following:
"Daniel Rudy" <sp******@spamt his.net> wrote
I have a peice of code that I'm making an attempt to code. The problem
is that I need to return an arbitrary number of char strings.

int function(char *fname, int *dcount, char *data[])

Would this work? If so, then how to you load the data into data? Malloc?


You can't do it that way.
The reason is that you don't know how many strings you have. Therefore you
need to call

char **ptr;
int N; / *set to number of strings */
ptr = malloc(N * sizeof(char *));

(in fact you would probably malloc() 1 and then call realloc() as you add
strings to the list, but that's just a detail).

The pointer therefore changes, so you can't pass it it. You need a char ***,
if you want to return it as a parameter.
A triple pointer is getting messy, so it's probably better to return the
pointer

char **function(char *fname, int *dcount)


I'm having trouble visualizing this. I thought that char **ptr is a
pointer to a pointer to a char (or array of chars). Am I wrong? I know
that it can be done, look at int main(int argc, char *argv[]) which
everyone knows. Hmm... I'm going to abandon this method and try
something else, which may also result in another post on declaring
nested unions and structures.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 2 '06 #5

"Daniel Rudy" <sp******@spamt his.net> wrote
I'm having trouble visualizing this. I thought that char **ptr is a
pointer to a pointer to a char (or array of chars). Am I wrong? I know
that it can be done, look at int main(int argc, char *argv[]) which
everyone knows. Hmm... I'm going to abandon this method and try
something else, which may also result in another post on declaring
nested unions and structures.

A pointer can point to anything.

If it points to an integer it is an int *, if it points to a character it is
a char *.
It can also point to another pointer. So a pointer to a pointer that points
to a character would be a char **. A pointer to that would be a char ***,
and so on (in practise you harldy ever need more than three levels).

In C, you work with arrays of values by pointing to the first element. So if
we have three integer in memory

12 15 -4

We can set a pointer, call it ptr, to point to the 12.

*ptr gives us the 12
*(ptr +1) gives us the 15
*(ptr +2) gives us the -4.

We can also use array notation

ptr[0] is exactly the same a *ptr
ptr[1] is exactly the same as (*ptr+1)

This is especially useful for strings, because we generally don't know how
long they will be. So you simply point to the first character in memory

a s t r i n g 0

we point to the 'a', and then we can examine all the other characters. The 0
tells us where the string ends.


Jan 3 '06 #6
"Malcolm" <re*******@btin ternet.com> writes:
[...]
We can also use array notation

ptr[0] is exactly the same a *ptr
ptr[1] is exactly the same as (*ptr+1)


Quibble: ptr[1] is the same as *(ptr+1).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 3 '06 #7
Hi, I think you're mostly right, but you wrote it in a way, that
could easily be mis-interpreted, especially by someone who
needs to have this explained ...

Malcolm <re*******@btin ternet.com> schrieb:
A pointer can point to anything.
Better: Pointers can point to any type of object in memory.
You can't point it to, say, the controller of your hard disk
;-)
If it points to an integer it is an int *, if it points to a character it is
a char *.
To further clarify, this does *not* mean that an int pointer
that accidentally points to a char object suddenly changes
its type from int* to char*. An int* is an int* and will remain
an int* as long as the program is running.

An expample:

struct { char str[10]; int i[10];} array;
char *pc = &array.str[9]; // takes the address of the last element in str
int *pi = &array.i; // address of the first (zero'th) element in i

Now incrementing pc like in:

pc = pc + 1;

does not change the type of the pointer. pc is still a char*
and when you try to do something with it, you get undefined
behaviour. The operation to increment a pointer to one place
_behind_ the last element of an array is perfectly legal and
well defined.

On the other hand:

pi = pi - 1;

does not change the type of pi from int* to char*, in fact
(even if it were allowed and defined what to do when you de-
crement a pointer to a place before the beginning of an array,
which it isn't) does not even point to str[9] but probably
to str[7]. In real life it can point anywhere, because of
padding bytes the compiler puts between the end of str[] and
the beginning of i[] to ensure that the elements contained
in i[] can be accessed (some systems cannot access integers
that reside on odd adresses, some can but extremely slow,
some will cause bus errors or protection faults, ....
it's undefined, _anything_ can happen).
It can also point to another pointer.
Additionally, a void* can point to _any_ type of object.
But that's about all that a void* can, or what you can
do with a void*. a void* can point to a void** (can it?)
but I have yet to discover of what use a void** could be.

Before you use it for something more trivial than just
pointing around, e.g. dereferencing it to access the object
it points to, you have to cast it to a pointer of a
particular type, meaning that _you_ have to know the type
of object it points to. The compiler cannot do that, it
relies on your knowing what you are doing. On some systems
(especially embedded controllers) this can be used for
some nasty tricks, but in 'standard' C this leads to
undefined behaviour.

The advantage of void* is that they can be
casted to any other type and the accesses made via the casted
(and therefore typed) pointer are guaranteed to succed in re-
retrieving a porperly typed object at the location it points
to. If you point a void* to the address of str[1] in the above
struct of my 'example' and then cast it to an int*, you'll
get undefined behaviour on most modern systems where
sizeof(char) != sizeof(int). The result of this misaligned
pointing is undefined, it you do nothing more than point
to objects which, on a particular system, cannot be stored
at this location nothing serious will happen, but if you
try to access the misaligned object, your program will
most certainly some kind of access error that will crash
your program.

to cast a void* to some other type of pointer, in C you
do not need the explicit cast like

int i;
void* pv=&i;
int *pi=(int*)pv;

it is sufficient to just write:

int *pi = pv;

The compiler selflessly does the conversion itself ('implicit'
cast).
*ptr gives us the 12
*(ptr +1) gives us the 15
*(ptr +2) gives us the -4.
Note that we increment the pointer by the number of objects.

The compiler knows their size and appropriately ensures that
the increment in memory locations is correct.
This is the reason why one can't do pointer-arithmetic
with void* pointers, because the objects the pointer points
to have no known type.
You can add pointers and integers, but not pointers and
pointers. With one exception: if both pointers are of the
same type (e.g. int*) and both point to locations inside
an int-array (up to one position after the last element
of the array) you can use p2-p1 to get the number of
elements between p1 and p2.
We can also use array notation

ptr[0] is exactly the same a *ptr
ptr[1] is exactly the same as (*ptr+1)
Typo: ptr[1] == *(ptr+1)
This is especially useful for strings, because we generally don't know how
long they will be.
Which is one of the reasons why one shouldn't use functions like
gets() or strcpy().
Oops, different thread. ;-)
a s t r i n g 0
better: "astring\0"
we point to the 'a', and then we can examine all the other characters. The 0
tells us where the string ends.


Markus, hoping that I didn't make too much mistakes at this
early time of day...
Jan 3 '06 #8

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

Similar topics

6
1878
by: benb | last post by:
I have form that looks a lot like a search bar for the user to search for records matching specified criteria (e.g. first names containing "ben"). For robust results, an intermediary form displays all records matching the criteria (hmm...sound like a popular web site I know). Here is the immediate problem: my code (below) works fine on my computer, but on some of those belonging to my work colleagues (who will be the end users of the db)...
11
1866
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine and blocks indefinitely. The net effect is that I cannot write a select on more than one file descripter if I want to block. (With timeout values >= 0, both Select() and Poll() work fine.)
13
5040
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
14
4849
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
89
6041
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
15
2287
by: amit.man | last post by:
Hi, i have newbie qestion, when i write #include <somthing.h> the precompiler subtitue that line with the lines from "somthing.h" header file. when, where and how the compiler insert the lines in "somthing.c" implementation file into my source code?
13
3547
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains some javascript code that calls window.open. I pass the resource url of page B to Page A's window.open call. Page B is then loaded and executed but none of the server-side code is rendered. If I view the source of the page, the code (and page...
7
2245
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something { private: int number; public: Something ();
55
6221
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
32
2720
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.