Return array of ponters?

P

priya

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
 
J

junky_fellow

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.
 
B

bjrnove

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;
}
 
J

junky_fellow

bjrnove said:
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;
}

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] ?
 
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] ?

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.
 
S

S.Tobias


[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;

/* 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!";
}
 
S

S.Tobias

bjrnove said:
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.
 
B

bjrnove

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:


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**.
 
P

priya

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 said:
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:
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**.
 
P

priya

Hi all,

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


Regards,
priya
 
J

junky_fellow

bjrnove said:
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 ?
 
K

Keith Thompson

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.
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.
 
K

Keith Thompson

bjrnove said:
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.
 
K

Keith Thompson

bjrnove said:
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).
 
J

junky_fellow

Keith said:
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 ?
 
K

Keith Thompson

Keith said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top