473,796 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing in structure pointers and returning structure pointers

41 New Member
Hi ,

Have the following case passing in structure pointers and returning structure pointers

can anyone spot whats wrong with this

structure name is structCar
Expand|Select|Wrap|Line Numbers
  1. void callfn( ){
  2.  
  3. structCar *Result;                               
  4. char var1[20], var2[20], var[20];
  5.  
  6. strcpy (var1,"test1");
  7. strcpy (var2,"test2");
  8. *Result = myfunc(var1,var2);
  9.  
  10. callfn2(&result,var);
  11.  
  12.  
  13. }
  14.  
  15.  
  16.  
  17.  
  18. structCar * myfunc(char *abc char *bef){
  19.  
  20.  
  21. resPtr =  (structCar*) malloc (sizeof(structCar) * 2); 
  22.  
  23. anotherfncall(&result, &abc, &bef)
  24.  
  25. resPtr = &result;
  26.  
  27. return resPtr; /*Returns a ptr to a structure */
  28. }
  29.  
  30.  
  31.  
  32. void callfn2(const structCar *, char *){
  33.  
  34. other stuff
  35.  
  36. }
  37.  
  38.  
  39.  
  40. anotherfncall(structCar *, const structCar *, const structCar *){
  41.  
  42. }
  43.  
Jan 30 '07 #1
9 1657
arne
315 Recognized Expert Contributor
Hi ,

Have the following case passing in structure pointers and returning structure pointers

can anyone spot whats wrong with this

structure name is structCar

void callfn( ){

structCar *Result;
char var1[20], var2[20], var[20];

strcpy (var1,"test1");
strcpy (var2,"test2");
*Result = myfunc(var1,var 2);

callfn2(&result ,var);


}




structCar * myfunc(char *abc char *bef){


resPtr = (structCar*) malloc (sizeof(structC ar) * 2);

anotherfncall(& result, &abc, &bef)

resPtr = &result;

return resPtr; /*Returns a ptr to a structure */
}



void callfn2(const structCar *, char *){

other stuff

}



anotherfncall(s tructCar *, const structCar *, const structCar *){

}

In function callfn() you're mixing upper and lower case ('Result' and 'result').
In function myfunc() the variable 'result' is not known.
And there are some more syntax errors.
I guess this is not a copy&paste of your code, is it?

But the main issue may be that myfunc() returns a (structCar *), which you try to assign to *Result, where you should use Result only, since it is of type (structCar *).

And the call to callfn2 should take Result and not &Result as its argument ... what did your compiler say?
Jan 30 '07 #2
horace1
1,510 Recognized Expert Top Contributor
it is difficult to comment as there are so many basic syntax errors in the code (missing , ; and undefined variables, etc)
however, a couple of things (and there are more)
Expand|Select|Wrap|Line Numbers
  1. structCar * myfunc(char *abc char *bef){
  2. resPtr = (structCar*) malloc (sizeof(structCar) * 2);
  3. anotherfncall(&result, &abc, &bef)
  4. resPtr = &result;
  5. return resPtr; /*Returns a ptr to a structure */
  6. }
  7.  
you allocate some memory using malloc() and point resPtr to it and two lines later assign the address of result to resPtr?

the second and third parameters to anotherfncall() are const structCar *
Expand|Select|Wrap|Line Numbers
  1. anotherfncall(structCar *, const structCar *, const structCar *){
  2.  
yet you call it
Expand|Select|Wrap|Line Numbers
  1. anotherfncall(&result, &abc, &bef)
where abc and bef are char *

I think you need to post the actual code
Jan 30 '07 #3
nano2
41 New Member
it is difficult to comment as there are so many basic syntax errors in the code (missing , ; and undefined variables, etc)
however, a couple of things (and there are more)
Expand|Select|Wrap|Line Numbers
  1. structCar * myfunc(char *abc char *bef){
  2. resPtr = (structCar*) malloc (sizeof(structCar) * 2);
  3. anotherfncall(&result, &abc, &bef)
  4. resPtr = &result;
  5. return resPtr; /*Returns a ptr to a structure */
  6. }
  7.  
you allocate some memory using malloc() and point resPtr to it and two lines later assign the address of result to resPtr?

the second and third parameters to anotherfncall() are const structCar *
Expand|Select|Wrap|Line Numbers
  1. anotherfncall(structCar *, const structCar *, const structCar *){
  2.  
yet you call it
Expand|Select|Wrap|Line Numbers
  1. anotherfncall(&result, &abc, &bef)
where abc and bef are char *

I think you need to post the actual code





This is what I have but I am getting problems with the compiler with the structure assignment any ideas here


Expand|Select|Wrap|Line Numbers
  1. main () {
  2.    structCar *resPtr;
  3.    resPtr = testfn();
  4.    testfn2(resPtr);
  5. }
  6.  
  7. structCar *testfn() {
  8.     structCar testfnRet;
  9.     return &testfnRet;
  10. }
  11.  
  12. int testfn2(StructCar ab) {
  13.     if (strcmp(testfn3(&ab,string) == 0)
  14.         return 1;
  15.     else 
  16.         return 0;
  17. }
  18.  
  19. char * testfn3(const structCar *, char *);
Feb 2 '07 #4
Ganon11
3,652 Recognized Expert Specialist
This is what I have but I am getting problems with the compiler with the structure assignment any ideas here


Expand|Select|Wrap|Line Numbers
  1. main () {
  2.    structCar *resPtr;
  3.    resPtr = testfn();
  4.    testfn2(resPtr);
  5. }
  6.  
  7. structCar *testfn() {
  8.     structCar testfnRet;
  9.     return &testfnRet;
  10. }
  11.  
  12. int testfn2(StructCar ab) {
  13.     if (strcmp(testfn3(&ab,string) == 0)
  14.         return 1;
  15.     else 
  16.         return 0;
  17. }
  18.  
  19. char * testfn3(const structCar *, char *);
I'm fairly certain there's a problem in testfn(). Within the function, you allocate memory for a structCar object, then return the address in memory of the object. But once the function is ended, testfnRet has gone out of scope, and so that place in memory will be de-allocated - thus, your pointer will point to a blank space in memory.
Feb 2 '07 #5
willakawill
1,646 Top Contributor
Hi. This is a better way to return a pointer.

Expand|Select|Wrap|Line Numbers
  1. structCar *testfn() {
  2.     structCar *testfnRet = new structCar;
  3.     return testfnRet;
  4. }
And, because we have used the 'new' operator:
Expand|Select|Wrap|Line Numbers
  1. main () {
  2.    structCar *resPtr;
  3.    resPtr = testfn();
  4.    testfn2(resPtr);
  5.    delete resPtr;
  6. }
Feb 2 '07 #6
Ganon11
3,652 Recognized Expert Specialist
Is the "new"/"delete" option availible in C, or just C++?
Feb 2 '07 #7
willakawill
1,646 Top Contributor
ooops!

Expand|Select|Wrap|Line Numbers
  1. structCar *testfn() {
  2.     structCar *testfnRet;
  3.     testfnRet = (structCar*) malloc (sizeof(structCar));
  4.     return testfnRet;
  5. }
Feb 2 '07 #8
nano2
41 New Member
ooops!

Expand|Select|Wrap|Line Numbers
  1. structCar *testfn() {
  2.     structCar *testfnRet;
  3.     testfnRet = (structCar*) malloc (sizeof(structCar));
  4.     return testfnRet;
  5. }

Thanks For this it has helped but the call to testfn2() is causing problems can you spot it ?

int res = testfn2(resPtr) ;
which has the following prototype
int testfn2 (StructCar ab)

Thanks
Feb 7 '07 #9
Ganon11
3,652 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. int testfn2(StructCar ab) {
  2.     if (strcmp(testfn3(&ab,string) == 0))
  3.         return 1;
  4.     else 
  5.         return 0;
  6. }
  7.  
  8. char * testfn3(const structCar *, char *);
It appears that there is an error in your if...branch. Is it just me, or did you never pass anything for the second argument of testfn3? It calls for a char*, but you pass it a string - string is not declared as a local variable, and you are not referencing it from any StructCar variable.

Also, there was a parentheses missing from the if condition. I have added this.

Finally, make sure you have actually defined testfn3 - all we see here is a prototype!
Feb 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
7645
by: Adam Balgach | last post by:
Greetings everyone, ive got a problem ive been working with for quite a while and need some help. ive got a structure: struct Data { char *data1; char *data2; int val1; int val2;
9
4813
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
25
2947
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
15
14882
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get the following error in the VB.net application: "An unhandled exception of Type
7
2643
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I make sure that my function is being exported by adding a line to the .def file. This seems to work because when I debug it recognizes the dll and once it hits the function it goes right into the proper location of the dll.
40
2371
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my libary do I create a function where user passes this callback function? How would I define the function?
7
2161
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but they do requrie much more memory than a simple pointer.
0
1404
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I know better than everyone who might want to use my module. Using values other than NULL might be unwise (the man page I'm looking at states that clearly) but isn't one of the core design philosophies of Python "we're all consenting adults"?
4
2638
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
9680
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
10456
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
10230
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
10012
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
9052
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
5442
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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
2926
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.