469,632 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

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 2304
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Poulos | last post: by
4 posts views Thread by Isaac | last post: by
23 posts views Thread by Nascimento | last post: by
7 posts views Thread by James Kanze | last post: by
127 posts views Thread by sanjay.vasudevan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.