473,799 Members | 3,081 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
16 2503

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**********@ya hoo.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**********@ya hoo.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_Keit h) 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**********@ya hoo.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_Keit h) 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_Keit h) 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**********@ya hoo.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**********@ya hoo.co.in writes:
Keith Thompson wrote:
ju**********@ya hoo.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_Keit h) 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
2939
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
2752
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
2044
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
4931
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
9686
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
10250
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...
0
10026
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
9068
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...
0
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.