Novice question about malloc and pointers
John Gordon
gordon at osiris.cso.uiuc.edu
Thu Apr 18 06:32:53 AEST 1991
trotter at ENUXHA.EAS.ASU.EDU (Russell T. Trotter) writes:
>I am trying to get an array of strings, therefore I am using the
>following declaration: char *str[MAX] where MAX is an arbitrary
>constant. My question is how do I allocate the memory for each
>character position? Do all the characters strings for each element
>in the array need to be allocated contiguously? The problem involves
>reading in lines of input. Each line would be stored as a string and
>the number of lines make up the number of elements in the array.
>Any information would be greatly appreciated. Thank you.
Here's how I did something similar: use a temporary variable
such as char buf[200] for getting each line of input, then do a
str[i] = (char *) malloc(strlen(buf) +1)
to copy each line into str.
---
John Gordon
Internet: gordon at osiris.cso.uiuc.edu #include <disclaimer.h>
gordon at cerl.cecer.army.mil #include <clever_saying.h>
More information about the Comp.lang.c
mailing list