Re: Newbie: Array of pointers to strings questions.




Longfellow wrote:
> Newbie here...
>
> Trying to wrap my head around what's going on here. Read through the
> FAQ until thoroughly confused. Got some insight from K&R2, no help
from
> D&D, H&S, CU or CT&P, at least that I could understand. So have done
> due diligence, I think.
>
> What I'm trying to do is extract information from one file and insert
> that information into other files. The first file has a string of
> records, each of which have an ID and for each, there exists another
> file. For each record, information is extracted and inserted into
the
> relevant file. There can be any number of pieces of information for
> each record, and the information in each case is extracted as a
string.
>
> So I figured that I need an array of pointers to each string that can
be
> created dynamically for each record as it is read. For the
insertion,
> the relevant file is read into a temp file, where the extracted
strings
> are inserted appropriately, and then the relevant file is removed and
> replaced (rename()) by the temp file. This is completed for each
record
> before moving on to the next.
>
> Works like a charm, but handling the array was a problem.
>
> The following code is an example of my undoubtedly very ugly
solution.
> I can't figure out why it works, nor can I comprehend what the code
> should be. I was paying attention to freeing allocated memory in a
> timely manner, which affected the solution somehow.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> const char *line1 = "This is line one.";
> const char *line2 = "This is line two.";
> const char *line3 = "This is line three.";
> const char *line4 = "This is line four.";
>
> char *array[4]
>
> char *ptr;
>
> printf("\nThis is the individual print-outs:\n");
>
> ptr = malloc(sizeof line1);
> strcpy(ptr, line1);
> array[0] = malloc(sizeof line1);
> array[0] = ptr;
> free(ptr);
>
> array[0] = malloc(sizeof line1);
> strcpy(array[0], line1);
> printf("array[0] is: %s\n", array[0]);
>
> ptr = malloc(sizeof line2);
> strcpy(ptr, line2);
> array[1] = malloc(sizeof line2);
> array[1] = ptr;
> free(ptr);
>
> array[1] = malloc(sizeof line2);
> strcpy(array[1], line2);
> printf("array[1] is: %s\n", array[1]);
>
> ptr = malloc(sizeof line3);
> strcpy(ptr, line3);
> array[2] = malloc(sizeof line3);
> array[2] = ptr;
> free(ptr);
>
> array[2] == malloc(sizeof line3);
> strcpy(array[2], line3);
> printf("array[2] is: %s\n", array[2]);
>
> ptr = malloc(sizeof line4);
> strcpy(ptr, line4);
> array[3] = malloc(sizeof line4);
> array[3] = ptr;
> free(ptr);
>
> array[3] == malloc(sizeof line4);
> strcpy(array[3], line4);
> printf("array[3] is: %s\n", array[3]);
>
> printf("\nThis is the main body print loop:\n");
> for (i=0; i<4; ++i)
> printf("array[%d] is %s\n", i, array[i]);
>
> printit();
>
> for (i=0; i<4; ++i)
> free(array[i]);
>
> free(ptr);
>
> return 0;
> }
>
> void printit() {
>
> int i;
>
> printf("\nThis is the 'printit()' function print loop:\n");
>
> for(i=0; i<4; ++i)
> printf("array[%d] is: %s\n", i, array[i]);
> }
>
> Running this produces:
>
> This is the individual print-outs:
> array[0] is: This is line one.
> array[1] is: This is line two.
> array[2] is: This is line three.
> array[3] is: This is line four.
>
> This is the main body print loop:
> array[0] is This is line one.
> array[1] is This is line two.
> array[2] is This is line three.
> array[3] is This is line four.
>
> This is the 'printit()' function print loop:
> array[0] is: This is line one.
> array[1] is: This is line two.
> array[2] is: This is line three.
> array[3] is: This is line four.
>
> Delete or change any part of the array handling sections and
something
> doesn't work correctly.
>
> What should the code be to produce the above results?
>
> Thanks all,
>
> Longfellow


ptr = malloc(sizeof line1);
strcpy(ptr, line1);

ptr will have the size of a memory address location in this case since
line1 is just a constant pointer to a character. Thus, on most 32-bit
machines ptr will have a size of 4 bytes, not sufficient enough to hold
the size of line1. What you could do is use strlen of line1 to
determine the size of what to allocate.

.



Relevant Pages

  • Re: How do I pull the info from this PostScript?
    ... Do you just want to extract the title from a file, ... When anchorsearch returns true, the 'post' string ... SourceArrayDecode filter from the array, and cvx exec the filter. ...
    (comp.lang.postscript)
  • Split Single Address Field into Component Parts
    ... would make a copy of the database before making any ... would subtract that part of the string from the working ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Split Single Address Field into Component Parts
    ... >address field using an update query. ... >string functions. ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Re: Help With Regular Expression (Another Post)
    ... How can I extract the State from the path and set that to a variable. ... Elseif s="Florida" Then ... 'if function returns an empty string then it means that no applicable state ... dim vState as Variant ...
    (microsoft.public.vb.general.discussion)
  • Newbie: Array of pointers to strings questions.
    ... What I'm trying to do is extract information from one file and insert ... and the information in each case is extracted as a string. ... the relevant file is read into a temp file, ... but handling the array was a problem. ...
    (comp.lang.c)