3

I've hit a wall with a program that uses embedded SQL to fetch rows from a database table, stores the row data in a struct, and then has that data processed with results being stored in another struct and pushed to a linked list. The struct that where the fetch data is stored is as follows:

struct rowstruct {
    char    *first;
    char    *last;
    long    amt;
}   client;

and my struct that I use to store the processed data (and subsequently push as a node in my linked list) is like this:

struct mystruct {
        char    *firstN;
        char    *lastN;
        long    total;
    }   data;

My problem is that with each fetch loop that occurs, I need to copy the client.first and client.last values into data.firstN and data.lastN, but I can't get it to work. The following, using the assignment operator, just seems to be copying the pointer, rather than the value:

data.firstN = client.first;
data.lastN = client.last;

If I output data.firstN and data.lastN after the first iteration of my loop, the values appear correct, but after the second fetch iteration, the first node in my list will reflect the values from the second fetch, rather than the first.

strcpy will compile, but fails at runtime due to segmentation fault, which from reading on here is due to the char* being used, though I don't think I can use char[] or string when fetching the data using embedded SQL, so that seems like a dead end.

I'm sure there's a way to do this, and it's probably obvious to most here, but I'm at a loss. Any help would be appreciated.

Thanks!

PSUlion01
  • 115
  • 2
  • 10
  • 1
    did you allocate memory for the `mystruct` `char *`s? ie `data.firstN = malloc (sizeNeeded)` - possibly via `strlen` - then `strcpy` note i would prefer `strncpy` or another safe alternative (`strcpy_s`?) – im so confused Oct 12 '12 at 18:34
  • 1
    `mystruct` seems to be redundant though – Alexander Oct 12 '12 at 18:39
  • Sorry about the confusion with mystruct. There's more to it than shown above... Didn't realize how confusing I made it in the post. – PSUlion01 Oct 14 '12 at 05:24
  • @AK4749 - See my comment below. I now see what I was doing wrong, and the suggested solution was pretty simple, though your comment makes it more clear how I'd go about allocating the memory for the char*'s. Thanks – PSUlion01 Oct 14 '12 at 05:26

1 Answers1

3

If the code says to copy the pointer, that is exactly what happens.

Probably what you want is something along the lines of

data.firstN = strdup (client.first);
data.lastN = strdup (client.last);

There are more C++-ish ways of doing the same thing, but this should get you over the hump.


You don't need to re-declare the structure. Instead, you can declare data with

struct rowstruct data;
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 1
    If you copy a string with `strdup`, be aware that you have to `free` it when you are done. Normally this would be done with `delete` in C++, but this is a C function that allocates it with `malloc` rather than `new`. http://linux.die.net/man/3/strdup – Geoff Montee Oct 12 '12 at 20:46
  • @wallyk - Thanks... The suggested solution per my professor was to declare firstN and lastN as char arrays, e.g. char firstN[30]. I was concerned about allocating unnecessary memory (or not enough), but since the data is coming from pre-defined db fields, he said it'd be ok to use the fixed char array size. I'm going to give your suggestion a try though just to see how it works. As for re-declaring the struct, mystruct had other members that weren't part of the original. – PSUlion01 Oct 14 '12 at 04:59
  • @Geoff - Thanks for pointing out the memory management piece of this. I need to spend a bit more time and really try to get a better grasp on everything in my program. I'm fairly new to programming, with no mastery or any single language. This particular task had me working with C code (due to the embedded SQL), yet I have more familiarity with C++, so I think my code is a bit Frankenstein right now. Once I'm done with it perhaps I can add it to this thread for commenting/suggestions? The best way to learn seems to be having others point out the problems and more preferred approaches. – PSUlion01 Oct 14 '12 at 05:07