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

Return array of ponters?

Hi all,

I am new to this group.I am working in c language.I have dount
in pointer? how to retun array of pointer in function?


example
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);
}

char* function(char *a[2])
{
char *b[2];
b=a;

return(b);
}
In this program i have to retun b value(array of pointer) to var
variable...
Thanks

regards,
priya

Nov 14 '05 #1
16 2455
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);
This is wrong. var is not an "lvalue".
}

char* function(char *a[2])
{
char *b[2];
b=a;
Again wrong. b is not an "lvalue".

return(b);
}
In this program i have to retun b value(array of pointer) to var
variable...

You cannot return an array of pointers. What you can do is return
the address of the first element of the array of pointers.

Nov 14 '05 #2

Hi.

You shouldn't work with an array of pointers like that. Instead you
should use
a pointer to a char* whitch will give you the result you want.

Your example would then look something like this.

example:
#include <stdio.h>

/* You should ALWAYS declare functions so the compiler knows what
argument it
wants. If not you basicly could give it as many arguments you want and
they all
would be the type int (witch in this case will work ok on most systems,
but not
all). */
char** function(char **a);

/* main always has the type int and if no arguments you should always
give void */
int main(void)
{
char *a[2]; /* Your array of pointers */
char **var; /* A pointer to a char* witch could work as an array of
pointers */
a[0] = "hi";
a[1] = "c language";
var = function(a);

printf("%s %s\n", var[0], var[1]);
system("pause");
return 0;
}

/* Takes one argument witch is a pointer to a char* and returns a
pointer to a char*. */
char** function(char **a)
{
char **b;

b=a;

/* You do not need the brackets, but that's not important */
return b;
}

--
bjrnove

Nov 14 '05 #3

bjrnove wrote:
Hi.

You shouldn't work with an array of pointers like that. Instead you
should use
a pointer to a char* whitch will give you the result you want.

Your example would then look something like this.

example:
#include <stdio.h>

/* You should ALWAYS declare functions so the compiler knows what
argument it
wants. If not you basicly could give it as many arguments you want and they all
would be the type int (witch in this case will work ok on most systems, but not
all). */
char** function(char **a);

/* main always has the type int and if no arguments you should always
give void */
int main(void)
{
char *a[2]; /* Your array of pointers */
char **var; /* A pointer to a char* witch could work as an array of pointers */
a[0] = "hi";
a[1] = "c language";
var = function(a);

printf("%s %s\n", var[0], var[1]);
system("pause");
return 0;
}

/* Takes one argument witch is a pointer to a char* and returns a
pointer to a char*. */
char** function(char **a)
{
char **b;

b=a;

/* You do not need the brackets, but that's not important */
return b;
}

--
bjrnove


I was just wondering how compiler evaluates var[0] and var[1].
var was originally declared as a double pointer to char.
How does compiler evaluates var[1] ?

Nov 14 '05 #4
>I was just wondering how compiler evaluates var[0] and var[1].
var was originally declared as a double pointer to char.
How does compiler evaluates var[1] ?


The compiler will translate var[1] into *(var + 1) witch in this case
will give you the address of a (since var is the address of a) +
sizeof(char*) (at least on a x86 system). I also think the standard
make shure this is true on every system, but I wouldn't bet my life on
it.

--
bjrnove

Nov 14 '05 #5
bjrnove <bj*****@gmail.com> wrote:

example:
[snipped some comments for clarity]
#include <stdio.h> char** function(char **a); int main(void)
{
char *a[2]; /* Your array of pointers */
char **var; /* A pointer to a char* witch could work as an array of
pointers */
a[0] = "hi";
a[1] = "c language";
var = function(a); printf("%s %s\n", var[0], var[1]);
system("pause"); This is very OS-specific, better use:
getchar(); return 0;
} char** function(char **a)
{
char **b; b=a; /* You do not need the brackets, but that's not important */
return b;
}


(I didn't test the code, but seems okay.)

The array `a' is passed to the function `function' by reference,
therefore there's no need to return a reference to it again.
Supposing `function' does something to the array, the code
could read:

void function(char **);

int main()
{
char *a[2] = {"hi", "c language"};
function(a);
printf("%s %s\n", a[0], a[1]);
}

void function(char **a)
{
a[0] = "Hello";
a[1] = "World!";
}

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #6
bjrnove <bj*****@gmail.com> wrote:
The compiler will translate var[1] into *(var + 1) witch in this case
will give you the address of a (since var is the address of a) +
sizeof(char*) (at least on a x86 system). I also think the standard
make shure this is true on every system, but I wouldn't bet my life on
it.


You safely could. This is how subscript operator is actually defined.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #7
>The array `a' is passed to the function `function' by reference,
therefore there's no need to return a reference to it again.
Supposing 'function' does something to the array, the code could read:
system("pause");

This is very OS-specific, better use:
getchar(); Oh, sorry. This was for me only (so I could see the result when
testing), I was supose to remove it when pasting the code :-).

void function(char **);
int main()
{
char *a[2] = {"hi", "c language"};
function(a);
printf("%s %s\n", a[0], a[1]);
} I though about do as you did above (with initializing), except I wanted
to make it as simular to the original example as possible. I think the
clue wasn't the code, but how to play with pointers to array's.



void function(char **a)
{
a[0] = "Hello";
a[1] = "World!";
}

This isn't what was asked for. The point here is to create a function
that returns an array of pointers. And I tried to explain that a char**
really is the same as char*[2] except sizeof would't work as expected
on the char**.

--
bjrnove

Nov 14 '05 #8
Hi all,

Thanks for your response...Now i am clear abt how to return
array of pointer in fuction...I runned ur sample program..it is working
good.....once again thanks
Regards,
priya
bjrnove wrote:
The array `a' is passed to the function `function' by reference,
therefore there's no need to return a reference to it again.
Supposing 'function' does something to the array, the code could
read:
system("pause");

This is very OS-specific, better use:
getchar();

Oh, sorry. This was for me only (so I could see the result when
testing), I was supose to remove it when pasting the code :-).

void function(char **);
int main()
{
char *a[2] = {"hi", "c language"};
function(a);
printf("%s %s\n", a[0], a[1]);
}

I though about do as you did above (with initializing), except I

wanted to make it as simular to the original example as possible. I think the clue wasn't the code, but how to play with pointers to array's.



void function(char **a)
{
a[0] = "Hello";
a[1] = "World!";
} This isn't what was asked for. The point here is to create a function
that returns an array of pointers. And I tried to explain that a

char** really is the same as char*[2] except sizeof would't work as expected
on the char**.

--
bjrnove


Nov 14 '05 #9
Hi all,

Thanks for ur response...I run ur sample program..it is working
good....once again thanks
Regards,
priya

Nov 14 '05 #10

bjrnove wrote:
I was just wondering how compiler evaluates var[0] and var[1].
var was originally declared as a double pointer to char.
How does compiler evaluates var[1] ?
The compiler will translate var[1] into *(var + 1) witch in this case
will give you the address of a (since var is the address of a) +
sizeof(char*) (at least on a x86 system). I also think the standard
make shure this is true on every system, but I wouldn't bet my life

on it.

--
bjrnove


How the sizeof(char *) is evaluated ?

Nov 14 '05 #11
On 23 May 2005 07:59:39 -0700, in comp.lang.c ,
ju**********@yahoo.co.in wrote:
How the sizeof(char *) is evaluated ?


Er? The compiler will determine the size of a pointer to a char.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #12
ju**********@yahoo.co.in writes:
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);


This is wrong. var is not an "lvalue".


Yes it is; it just isn't a modifiable lvalue.
}

char* function(char *a[2])
{
char *b[2];
b=a;


Again wrong. b is not an "lvalue".


Again, b is an lvalue, but not a modifiable lvalue.
return(b);
}
In this program i have to retun b value(array of pointer) to var
variable...

You cannot return an array of pointers. What you can do is return
the address of the first element of the array of pointers.


Yes, you can return the address of the first element of an array, but
if that array is local to the function, you'll be returning the
address of something that won't exist by the time the caller sees it.
Any attempt by the caller to use the pointer will invoke undefined
behavior. (In many cases, the error won't be detected, and the caller
will happy mess around with the memory where the object used to
exist.)

For example:

char *func(void)
{
char result[10];
strcpy(result, "Oops");
return result; /* Bad idea! */
}

...

This returns a pointer to "result", an object that ceases to exist as
soon as the return is executed. Many compilers will issue a warning
for this error.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #13
ju**********@yahoo.co.in writes:
bjrnove wrote:
>I was just wondering how compiler evaluates var[0] and var[1].
> var was originally declared as a double pointer to char.
>How does compiler evaluates var[1] ?


The compiler will translate var[1] into *(var + 1) witch in this case
will give you the address of a (since var is the address of a) +
sizeof(char*) (at least on a x86 system). I also think the standard
make shure this is true on every system, but I wouldn't bet my life
on it.


How the sizeof(char *) is evaluated ?


sizeof(char *) yields the size, in bytes, of an object of type char*
(a pointer to char). The compiler has to know how big it is, so the
expression is evaluated at compilation time.

But it's slightly misleading to say that var+1 evaluates to the
address of a + sizeof(char*). Let's assume sizeof(char*) is 4 (which
is a typical value). Given that var points to a, and that var is of
type char**, arithmetic on var inherently works in units of 4, so
var+1 points 4 bytes past the location where var points.

Any decent C textbook (K&R2 is good) should explain all this. Much of
it is also covered in the C FAQ.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #14
"bjrnove" <bj*****@gmail.com> writes:
[...]
This isn't what was asked for. The point here is to create a function
that returns an array of pointers. And I tried to explain that a char**
really is the same as char*[2] except sizeof would't work as expected
on the char**.


No, a char** is *not* really the same as a char*[2]. The former is a
pointer to a pointer; the latter is an array of pointers. The only
link between them is that an expression of array type, in most
contexts, is automatically converted to a pointer to the first element
of the array.

A function cannot return an array value (unless it's wrapped in a
struct). The usual way to do the equivalent of returning an array
value is to return a pointer to the array's first element -- but then
you have to worry about how the array itself is allocated.

See the C FAQ <http://www.eskimo.com/~scs/C-faq/top.html>, particularly
sections 4 (Pointers) and 6 (Arrays and Pointers).

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #15

Keith Thompson wrote:
ju**********@yahoo.co.in writes:
main()
{
char *var[2];
char *a[2];
a[0]="hi";
a[1]="c language";
var=function(a);


This is wrong. var is not an "lvalue".


Yes it is; it just isn't a modifiable lvalue.


I have a confusion about how "var" could be an lvalue? The
compiler doesn't allocate any storage to "var" , so we can't
store any value in it. So, we shouldn't call it an "lvalue".
Even when I tried to compile, my compiler gave me the error
" "var" is not an lvalue "
Is there something I am missing ?

Nov 14 '05 #16
ju**********@yahoo.co.in writes:
Keith Thompson wrote:
ju**********@yahoo.co.in writes:
>> main()
>> {
>> char *var[2];
>> char *a[2];
>> a[0]="hi";
>> a[1]="c language";
>> var=function(a);
>
> This is wrong. var is not an "lvalue".
Yes it is; it just isn't a modifiable lvalue.


I have a confusion about how "var" could be an lvalue? The
compiler doesn't allocate any storage to "var" , so we can't
store any value in it. So, we shouldn't call it an "lvalue".


What makes you think no storage is allocated for "var"? It's an array
of two pointers to char. If sizeof(char*) is 4, for example, then 8
bytes will be allocated for "var".

An lvalue is an expression that designates an object.

On the other hand, an array expression such as the name of an array
variable, is implicitly converted (in most contexts) to a pointer to
its first element. It's an lvalue before the conversion, but not
after the conversion.
Even when I tried to compile, my compiler gave me the error
" "var" is not an lvalue "


That's an odd message, but I suppose you could argue that var is no
longer an lvalue after the implicit conversion.

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #17

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

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
8
by: ptek | last post by:
Hi all, I'm quite new to pointers, so this might be a silly question :S I need to allocate an array of pointers to unsigned char type... So, if I needed instead to allocate an array of...
5
by: shashi59 | last post by:
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
7
by: James Kanze | last post by:
On Apr 10, 4:06 pm, Jerry Coffin <jcof...@taeus.comwrote: Just a note, but in all of these languages, *all* objects are dynamically allocated. I wonder if this isn't more what caused you...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.