James Kuyper
8/29/2008 8:48:00 PM
stevenr...@gmail.com wrote:
> Hello All,
>
> I am pretty rusty when it comes to C. I am reading in input and
> trying to create of vector of pointers to store data based on spaces.
> I understand the idea of pointers, but I am having issues
> understanding on how to use char **. Below is the code I am running.
> In the code, I am storing input from the read command for traversing.
> How do I obtain the values from the array that is being referenced
> through the variable vPointer to? This is input from the user
> (characters).
This question properly belongs in comp.lang.c. I'm cross-posting this
response, and redirecting follow-ups to comp.lang.c only.
> int main()
> {
> char **vPointer;
> char *tmp2;
> char buffer[125];
>
> write(1, "Enter values", 10);
write() is not a standard C library routine. You'd be better off using
puts() or printf(), at least as far as posting to either of these
newsgroups is concerned.
> read(...);
read() is also not a standard C library routine, you should use
fread() or fgets() or fscanf(), depending upon what you actually want
to doe. But more importantly, that is clearly not a legal function
call. I presume that it represents a much larger piece of code that
actually does something useful. Possibly it does something to tmp2, or
buffer, neither of which seems to be doing anything useful at this
point. In order to answer your question properly, we need to know what
that code actually does.
> vPointer = malloc(125 * sizeof(char *);
At this point you've got a pointer to an uninitialized array of 125
pointers to char. You can't make any safe use of them until after
you've initialized them.
> int i;
> for(i=0; i < 125; i++)
> {
> printf("Printing values referenced from vPointer %c",
> **vPointer[i]);
Here, you try to print out those pointers, without ever having
initialized them. The behavior is undefined.
What you need to do is allocate memory that each of those pointers
will point to, and then set them to point at that memory. How you
should do that depends upon details you haven't given us about what,
precisely, it is that you are trying to do.
> }
>
>
> }