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,var2);
-
-
callfn2(&result,var);
-
-
-
}
-
-
-
-
-
structCar * myfunc(char *abc char *bef){
-
-
-
resPtr = (structCar*) malloc (sizeof(structCar) * 2);
-
-
anotherfncall(&result, &abc, &bef)
-
-
resPtr = &result;
-
-
return resPtr; /*Returns a ptr to a structure */
-
}
-
-
-
-
void callfn2(const structCar *, char *){
-
-
other stuff
-
-
}
-
-
-
-
anotherfncall(structCar *, const structCar *, const structCar *){
-
-
}
-
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?
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) -
structCar * myfunc(char *abc char *bef){
-
resPtr = (structCar*) malloc (sizeof(structCar) * 2);
-
anotherfncall(&result, &abc, &bef)
-
resPtr = &result;
-
return resPtr; /*Returns a ptr to a structure */
-
}
-
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 * -
anotherfncall(structCar *, const structCar *, const structCar *){
-
yet you call it -
anotherfncall(&result, &abc, &bef)
where abc and bef are char *
I think you need to post the actual code
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) -
structCar * myfunc(char *abc char *bef){
-
resPtr = (structCar*) malloc (sizeof(structCar) * 2);
-
anotherfncall(&result, &abc, &bef)
-
resPtr = &result;
-
return resPtr; /*Returns a ptr to a structure */
-
}
-
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 * -
anotherfncall(structCar *, const structCar *, const structCar *){
-
yet you call it -
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 - main () {
-
structCar *resPtr;
-
resPtr = testfn();
-
testfn2(resPtr);
-
}
-
-
structCar *testfn() {
-
structCar testfnRet;
-
return &testfnRet;
-
}
-
-
int testfn2(StructCar ab) {
-
if (strcmp(testfn3(&ab,string) == 0)
-
return 1;
-
else
-
return 0;
-
}
-
-
char * testfn3(const structCar *, char *);
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 - main () {
-
structCar *resPtr;
-
resPtr = testfn();
-
testfn2(resPtr);
-
}
-
-
structCar *testfn() {
-
structCar testfnRet;
-
return &testfnRet;
-
}
-
-
int testfn2(StructCar ab) {
-
if (strcmp(testfn3(&ab,string) == 0)
-
return 1;
-
else
-
return 0;
-
}
-
-
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.
Hi. This is a better way to return a pointer. - structCar *testfn() {
-
structCar *testfnRet = new structCar;
-
return testfnRet;
-
}
And, because we have used the 'new' operator: - main () {
-
structCar *resPtr;
-
resPtr = testfn();
-
testfn2(resPtr);
-
delete resPtr;
-
}
Ganon11 3,652
Recognized Expert Specialist
Is the "new"/"delete" option availible in C, or just C++?
ooops! - structCar *testfn() {
-
structCar *testfnRet;
-
testfnRet = (structCar*) malloc (sizeof(structCar));
-
return testfnRet;
-
}
ooops! - structCar *testfn() {
-
structCar *testfnRet;
-
testfnRet = (structCar*) malloc (sizeof(structCar));
-
return testfnRet;
-
}
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
Ganon11 3,652
Recognized Expert Specialist - int testfn2(StructCar ab) {
-
if (strcmp(testfn3(&ab,string) == 0))
-
return 1;
-
else
-
return 0;
-
}
-
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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...
|
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;
}
|
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
|
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.
| |
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?
|
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.
|
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"?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |