Jake Cooper
Jake Cooper

Reputation: 109

Trying to store the return of a char * function into a char array

char* getStationName(int stationID) {

    if (stationID < 0 || stationID >= MAX_STATIONS || !AllStationNames[stationID]) 
        return "Unknown";

    return AllStationNames[stationID];
}

AllStationNames function is here: (http://pastebin.com/zmmrUXTM) It is an array of strings:

char *AllStationNames[MAX_STATIONS] = {
[1] = "Ian Stewart Complex/Mt. Douglas High School",
[3] = "Strawberry Vale Elementary School",
...
[197] = "RASC Victoria Centre",
[199] = "Trial Island Lightstation",
[200] = "Longacre",
};

I'm trying to input that integer, and retrieve the string using this function, then store it into a struct, kinda looks like this.

MapMarker mapInfo[t];
int k;
for(k=0; k < MAX_STATIONS; k++) {

    char* returned_str = getStationName( stationInfo[k].stationID );
    mapInfo[k].markerName = returned_str;
}

This gives me a pointer without cast error. Really have no idea what to do from here.

Upvotes: 0

Views: 61

Answers (2)

iamacomputer
iamacomputer

Reputation: 565

there are many solutions to this problem.

if you know the max length of the station name, your struct could have

struct MapMarker {
...

char markerName[MAX_STATION_NAME_LENGTH];
} ;

and then you can strcpy (mapInfo[k].markerName, returned_str);


if you don't now the max length then you could strdup it:

mapInfo[k].markerName = strdup(returned_str); (markerName would need to be a char *)

but then you need to free it properly.


if you have the option of c++ you could use a std::string

struct MapMarker {
...
std::string markerName;
} ;

in which case your code would work correctly.


if you know all of your station names will be the same through the execution of your program, you actually don't need to copy it.

except you should probably use

const char *, instead of char *

(markerName would be const char *)


in my experience with strdup, it has some wackiness with memory allocation on some systems... so you might want to do something like:

char *myStrdup (char *s)
{
   char *r = malloc(strlen(s)+1);
   strcpy(r, s);
   return r;
}

hope this helps

Upvotes: 0

Dai
Dai

Reputation: 155055

According to your comment, the definition of MapMarker is such:

struct MapMarker {
    float latitude;
    float longitude;
    char markerName[100];
};

In this case, markerName exists as a value member of MapMarker, markeName is not a pointer to a string, but a string itself (with a fixed length of 100 elements).

If you're using C, then you'll have to use strcpy, and also make sure you're disposing of MapMarker instances correctly if you're allocating them on the heap:

char* stationName = getStationName( stationInfo[k].stationID );
strcpy( &mapInfo[k].markerName, stationName );

I'll suffix my answer with a dire-warning to avoid C-style string processing functions because they're unsafe w.r.t.: buffer-overflows (e.g. what if stationName is longer than 100 bytes? and don't forget about null terminators).

Upvotes: 1

Related Questions