Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) Struct as parameters (Read 6608 times)
gj
Junior Member
**
Offline


Posts: 42
Joined: Feb 12th, 2016
Struct as parameters
Oct 18th, 2016 at 10:12pm
Print Post  
Hello,

I did quite some googling on this but not able to find out what's going wrong. I followed the guidelines in this post: http://forum.arduino.cc/index.php?topic=44890.0
I have this function that extracts the HTTP-status and -version from a GET-response that I received from a webserver.

This is the actual parse-function (which is in a CPP-file where the rest of the class is also defined):
Code
Select All
void xBee::parseHTTPResponse(struct httpresponse* response) {
char* statusLine;
char* cr = "\13";

	// First, extract only the part until the first CRLF
	statusLine = strtok(_respBuffer, cr);

	char* httpVersion;
	httpVersion = strtok(statusLine, " ");			// This is like "HTTP/1.1"
	response->status = atoi(strtok(NULL, " "));		// This should be like 200, 403 or such

	// Parse the version
	strtok(httpVersion, "/");						// This is "HTTP", ignore
	strcpy(response->version, strtok(NULL, "/"));	// Get the version
} 



The struct is defined in a H-file with the same name as the CPP-file.
The struct is defined as:
Code
Select All
typedef struct httpresponse {
	char version[3];
	int status;
} 


Everything compiles OK.

When I put debug-breaks in I get the right results (inside the function), eg. status=200 and version="1.1". However, the calling program gets only zero's.
(The variable "_respBuffer" is a private 64-char in the class where the response from the webserver is stored).

In the calling program I have:
Code
Select All
httpresponse httpResult; 


and the actual call:
Code
Select All
xb->parseHTTPResponse(&httpResult); 



After the call a Serial.println(httpResult.status) will always return "0" (although, as said, in the function 'response->status' was 200, which is correct).

Any thoughts?
« Last Edit: Oct 19th, 2016 at 1:09pm by gj »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Struct as parameters
Reply #1 - Oct 20th, 2016 at 8:36pm
Print Post  
Did you try httpResult->status
  
Back to top
WWW  
IP Logged
 
gj
Junior Member
**
Offline


Posts: 42
Joined: Feb 12th, 2016
Re: Struct as parameters
Reply #2 - Oct 21st, 2016 at 6:19pm
Print Post  
Yes, but that gives the infamous red error-mark below 'httpStatus'.
Which I think is logical because in the main-routine httpStatus is not a pointer but an actual variable of the struct-type.

As you can see in the parse-routine I DO use the -> because there I'm using the references (pointers) to the httpStatus-var that was declared in the calling routine.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Struct as parameters
Reply #3 - Oct 21st, 2016 at 6:32pm
Print Post  
Why NULL instead of using statusLine to fill the response status?
  
Back to top
WWW  
IP Logged
 
gj
Junior Member
**
Offline


Posts: 42
Joined: Feb 12th, 2016
Re: Struct as parameters
Reply #4 - Oct 23rd, 2016 at 5:17pm
Print Post  
Tim@Visual Micro wrote on Oct 21st, 2016 at 6:32pm:
Why NULL instead of using statusLine to fill the response status?


I'm not exactly sure what you mean...

I could probably use ''statusLine" to fill the response-status?
But would that solve my issue?
It might save a variable, which is always a good consideration, but first I would like to understand why I'm not getting the results (outside the function, that is) with the current code.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Struct as parameters
Reply #5 - Oct 24th, 2016 at 7:31pm
Print Post  
I am not familiar with the methods you are using except to say that I can't see how the status can be populated from your code. I also don't know the format of the respbuffer string

A simple way might be to use the Arduino String object to get parts of the string.

https://www.arduino.cc/en/Reference/StringObject
  
Back to top
WWW  
IP Logged
 
gj
Junior Member
**
Offline


Posts: 42
Joined: Feb 12th, 2016
Re: Struct as parameters
Reply #6 - Oct 24th, 2016 at 8:39pm
Print Post  
The basic question is: How do I return values in the struct to the calling program?
The routine does what it needs to do (it populates the values for response->status and response->version correctly) but the calling program doesn't get the results back.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Struct as parameters
Reply #7 - Oct 24th, 2016 at 8:49pm
Print Post  
Don't know. You would think that passing a struct to a function means that caller owns the struct.

The forum is really just for the plugin. The arduino.cc forum and stackoverflow are the best place for code questions. Sorry I didn't make that point earlier.
  
Back to top
WWW  
IP Logged
 
gj
Junior Member
**
Offline


Posts: 42
Joined: Feb 12th, 2016
Re: Struct as parameters
Reply #8 - Oct 26th, 2016 at 12:32pm
Print Post  
Yeah. I will post in Arduino-forum. Tnx anyway.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint