473,695 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2488
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.Be dBuG.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.Be dBuG.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

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

Similar topics

5
2931
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]; Array.prototype.findValue = function(val) { // blah }
0
6457
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, 'b_id' => 9, 'c_id' => 3,
4
157991
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
6605
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 oci8 routines. The output resides in a structure defined within the sub-routine. Now I want this structure to be returned to main program so that I can assing output data to variables in main program and do some manipulation. Can any body guide...
23
3593
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
17715
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 unsigned chars, i'll do this :
5
2741
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
2034
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 problems, rather than garbage collection. In a language in which all objects are dynamically allocated, garbage collection is almost a necessity. Can you imagine what
127
4875
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
8630
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8982
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
8845
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
8825
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
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
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
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.