Pages: [1]   Go Down
Author Topic: Simple code is not working as expected  (Read 454 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to make "b" point to "a". If I do it inline, then everything is ok. But if inside function the "b" fails to point to "a".

Code:
void D(char* address, char* port)
{
port = address;
}

void loop()
{
char a[50] = "bla bla bla";
char* b;
//b = a;
D(a,b);
Serial.println(a);
Serial.println(b);
delay(1000);

Can anyone explain? Thanks ahead.
Logged

italy
Offline Offline
Full Member
***
Karma: 3
Posts: 223
Muuuuu
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what is D(a,b) for?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's just for testing purposes. It sould make "port" point to same as "address". So the programs prints two equal lines. But it doesn't.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

port and address are local to the D() function. The values that they contain just happen to be addresses. Changing their value (as in the address value contained within the local variable) isn't going to extend beyond the function as you've seen. If you want to change the address of port, you need to pass a reference to it. This is isn't any different than passing any other numeric variable's value and expecting a change to the numeric value outside of the function(), you're just dealing with a char pointer data type.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, seems I get it. I need to make function like this
Code:
void F(char* address, char** port)
{
   *port = address;
}
Thanks again.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I get value from char** A ?

**A[0] is not working.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

**A[0] is not working.
Each * is a derefence and the
Code:
[0]
is also a deference, so that gives you 3 differences. What does your declaration of A look like now? Better yet, what does your updated code look like?
« Last Edit: November 20, 2012, 12:32:43 pm by Arrch » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to split a string to Address and Port and return 1 if address starts with a number
Code:
bool isIPAddress(char** address, char** port)
{
strsep(address,":");
*port = *address;
strsep(port, ":");
*port = strsep(port, ";");
return ((57 - **address[0]) >= 10);
}

void loop()
{
char a[50] = "CONNECT:192.168.1.1:8000;";
char* b;
//b = a;
char* c = a;
Serial.println(isIPAddress(&c, &b));
Serial.println(c);
Serial.println(b);
delay(1000);
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3379
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to split a string to Address and Port and return 1 if address starts with a number
Code:
bool isIPAddress(char** address, char** port)
{
strsep(address,":");
*port = *address;
strsep(port, ":");
*port = strsep(port, ";");
return ((57 - **address[0]) >= 10);
}

void loop()
{
char a[50] = "CONNECT:192.168.1.1:8000;";
char* b;
//b = a;
char* c = a;
Serial.println(isIPAddress(&c, &b));
Serial.println(c);
Serial.println(b);
delay(1000);
}

One too many deferences on address.

Seems strtok() would be more appropriate here. First call gives you "CHANNEL" second gives you the IP and the third gives you the port.
If you don't want to destroy your original string, then you can make a copy, and pass a reference for the port and the address and assign your result to the results of strtok() to them. Just off the top of my head.
Logged

Pages: [1]   Go Up
Jump to: