Reputation: 1081
How do I access individual chars in a pointer char array? I have an array which might look something like this:
064951.000
A
2307.1256
N
12016.443
E
0.03
165.48
260406
In the function calling my function in question:
char *parsedString[9];
getParsedString(parsedString);
size_t len = strlen(parsedString[3]);
pos.latitude = parseCoordinatePart(len, parsedString[3]);
Heres the function where I have problem understanding how to do:
double parseCoordinatePart(int len, char partArray[]){
if(len == 8){
degrees = 10 * partArray[0] - '0';
degrees += partArray[1] - '0';
}else if(len == 9){
indexAdd = 1;
degrees = 100 * partArray[0] - '0';
degrees += 10 * partArray[1] - '0';
degrees += partArray[2] - '0';
}
//More calculations to get longitude or latitude
}
While parseCoordinatePart() does not give any compile errors I can not call it from my function above. My first thought was to dereference parsedString[3] but anyway I tried it always gave me "illegal conversion of integer to pointer"
Update: Heres the code for setting parsedString data:
void splitNmeaString(char *parsedString[]){
char *token;
// remove the first token
token = strtok(receivedData, ",");
int tokenIndex = 0;
while( token != NULL )
{
token = strtok(NULL, ","); // works when 1st arg is NULL. But why?!
if(tokenIndex < 10 ){
parsedString[tokenIndex] = token;
}else{
break;
}
}
}
Upvotes: 0
Views: 832
Reputation: 27702
One problem is you are multiplying an invalid value by its positional factor. Since multiplication has a higher precedence than subtraction, you should subtract '0' to your digit character before multiplying it:
double parseCoordinatePart(int len, char partArray[]){
if(len == 8){
degrees = 10 * (partArray[0] - '0'); //Here
degrees += partArray[1] - '0';
}else if(len == 9){
indexAdd = 1;
degrees = 100 * (partArray[0] - '0');
degrees += 10 * (partArray[1] - '0');
degrees += partArray[2] - '0';
}
//More calculations to get longitude or latitude
}
Did you notice that parsedString[3]
is not a string representation of a number but a cardinal point? N
.
None of your parseCoordinatePart
if branches will be executed.
Upvotes: 1
Reputation: 23236
Too much for a comment, but:
Unless you are doing it in getParsedString()
, Somewhere in your code char *parsedString[9];
has to be allocated memory for all 9 elements of the char *[9]
for(i=0;i<9;i++)
{
parsedString[i] = malloc(20);
}
Are you doing this?
Edit (per comment)
When I allocated memory, and populated at least one string to test, function called without error, as shown:
int main ()
{
//char *parsedString[9];
//given that your system may not support malloc/calloc
char parsedString[9][20]; //adjust second index if need more space
parsedString[1] = malloc(20);
//getParsedString(parsedString); //not defined for me, comment
strcpy(parsedString[1], "sdfgsd"); //fill with anything to test call
size_t len = strlen(parsedString[1]);
parseCoordinatePart(len, parsedString[1]); //calls without error given previous lines
return 0;
}
Note, I have only allocated memory for, and populated one of the nine elements of char *parsedString[9];
to test the function call. It now calls without error as shown.
I do not see the definition of getParsedString(parsedString);
, but suspect that you are doing memory allocation and string propulation there? If not, that may be where part of the problem lies.
Upvotes: 1