Go Down

Topic: Simple code is not working as expected (Read 507 times) previous topic - next topic

Raptor5

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: [Select]
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.

cantore


Raptor5

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.

Arrch

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.

Raptor5

Ok, seems I get it. I need to make function like this
Code: [Select]

void F(char* address, char** port)
{
   *port = address;
}

Thanks again.

Raptor5

How can I get value from char** A ?

**A[0] is not working.

Arrch

#6
Nov 20, 2012, 06:26 pm Last Edit: Nov 20, 2012, 06:32 pm by Arrch Reason: 1

**A[0] is not working.

Each * is a derefence and the
Code: [Select]
[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?

Raptor5

I want to split a string to Address and Port and return 1 if address starts with a number
Code: [Select]
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);
}

Arrch


I want to split a string to Address and Port and return 1 if address starts with a number
Code: [Select]
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.

Go Up