473,786 Members | 2,380 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 2500
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
2938
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
6473
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
157996
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
6614
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
3615
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
17721
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
2750
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
2042
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
4927
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
9492
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
10163
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
10108
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
9960
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
8988
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...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
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
3
2894
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.