[c] malloc with struct->char **variablelengtharray
I've had a bad case of coders block for quite some time, and honestly, I'm just tired of searching, reading,reviewing,examing with gdb, cgdb, ddd...
Anyhoo, the idea goes something like this. Code:
typedef struct SOME_KIND_OF_STRUCT Code:
SOME_KIND_OF_TYPE *skot; Then I should be able to use/test skot->arraycount in a for loop to point to each string element stored at the end of the malloc'ed space for each possible skot->array[x] (Though, by looking at the code, it looks like it may create an extra pointer to which I could set to NULL for testing the end of skot->array[x] and wouldn't need skot->arraycount). Sooooo! Yay or nay? Good or bad? Or was the implied question not explanatory enough? Or was it purely stupid of me to ask? Heh! |
It is not correct to use char **array; for that.
I always forget which compilers accept char *array[]; vs. which accept char *array[0]; but one of those is correct for what you seem to be trying to do. Quote:
After that, I'm lost on what you think you're doing, partially because I don't know what listofstrings is supposed to be. strncpy stops at either the first '\0' (end of first string?) or the specified length, whichever comes first. I think I understand what kind of struct you intend to create and what sort of contents it should have. But I don't see how you intend to populate those contents. |
Quote:
Quote:
The usual way is to allocate the struct first: Code:
skot = (SOME_KIND_OF_TYPE*)malloc(sizeof(SOME_KIND_OF_TYPE)); Code:
skot->arraycount = arraycount; Code:
for (size_t i = 0; i < skot->arraycount; i++) { You can probably do it the way you attempted with some kind of very ugly hacks, (and you would also have to set each pointer in the struct to point somewhere), but I would not recommend this way unless you have a very good reason to do so and you know exactly what you're doing. |
Quote:
<Edit> The text I wrote was horrible, and should have been explained as follows. As long as malloc creates a contiguous space, and as long as the programmer takes into account the number and size of elements. Then she/he should be able to populate the pointers to the proper locations within the malloc'ed space. </Edit> Hopefully the next part will explain that (in a way that I happen to understand it). Bad assembly representation below. Code:
SOME_KIND_OF_STRUCT: And after malloc (somwhere unbeknownst to the programmer in memory this space should be created). Code:
skot: Then the position of the string list should be at sizeof(SOME_KIND_OF_STRUCT)+sizeof(char *)*arraycount (give or take 1). Quote:
Quote:
It just seems odd to re-call malloc to create a new space which should be found easily by adding, subtracting where in memory the malloc'ed struct was created. Erf! I've lost my train of thought. If this doesn't help, I'll attempt it again later. Anyway, thanks so far. You helped me with some things that have been 'blocking' me, and now know to stop using strncpy. |
I think this is what you are trying to do (the parts in red are a test harness around the part you want).
Code:
#include <stdio.h> |
Quote:
I know that efficiency on that scale usually doesn't matter. So I understand your objection to strange coding that just achieves a little extra efficiency. But if you only try efficient coding when you really need it, you won't have the right experience to get it right when you really need it. Quote:
Quote:
Quote:
|
Quote:
Quote:
<Edit 2> This was explained as incorrect below by ntubski. Tested it, and it is incorrect. Oh! By the way, Code:
// Shouldn't this <Edit> And the finished product with some modifications looks like this...(Including one modification learned from ntubski. Don't know about offsetof, have read through the man page, but getting late and will read more on it later). Code:
|
Quote:
Code:
typedef struct SOME_KIND_OF_STRUCT { Quote:
Code:
array[i] === *(array + i) |
Quote:
Both your memcpy, and the loop you have following it assume the strings are stored sequentially. Those will go very wrong if the strings are not stored that way. Look at my example (post #5), for how to define a collection of sequentially stored strings. That approach lets you correctly memcpy the whole set at once and it lets the compiler count up the total (vs. your example where you manually counted the total). As you can see in my code, that requires code using strlen to compute the result pointers. If you prefer input as an array of char*, then don't assume the text is all stored contiguously (so don't try to copy it all in one memcpy). Instead, you can use a loop using strlen with either strcpy or memcpy, to compute each result pointer and copy each string. If the input is in that form, I would also suggest a preview loop to compute the total text size. Manually counting it is lame. |
Quote:
Something like as follows. (untested). Code:
FILE *fh; <Edit> Alright, here is the final working example (on my machine with my version of gcc at this moment in time, anyway) minus any ambiguity, hopefully. Code:
|
All times are GMT -5. The time now is 10:35 AM. |