C, store strings in an array

Get your foo on.

C, store strings in an array

Postby nadir » May 19th, 2012, 2:55 pm

Little problem here.
Either i don't fully understand "char **name" or i got a lack of logic. Or something i don't think of.
To figure out the error the last, and short, code-block should be sufficient.

If i do this:
using a char **list to store strings
allocate memory to it
Code: Select all
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
   char *prog = argv[0];
   
   char **linelist;
   int listlen = 3;
   int index = 0;

   char *line;
   int linelen = 80;
   int len = 0;


   /*allocate memory*/
   /*****************/
   linelist = calloc(sizeof(char **), listlen);
   if (linelist == NULL) {
      fprintf(stderr, "%s: Error. Could not allocate memory\n", \
            prog);
      exit(42);
   }

   line = malloc(linelen);
   if (line == NULL) {
      fprintf(stderr, "%s: Error. Could not allocate memory\n", \
            prog);
   }

   /*fill it manually*/
   /******************/
   linelist[0] = "foo";
   linelist[1] = "bar";
   linelist[2] = "baz";


   /*print result on stdout, dummy*/
   /*******************************/
   for (index = 0; index < 3; ++index) {
      printf("%s\n", linelist[index]);
   }

   
   return 0;
}

it works. As far i see.

If i do this:
using a char **list
allocate memory to it
using getline to get input from stdin
allocate memory to each line:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
   char *prog = argv[0];
   
   char **linelist;
   int listlen = 3;
   int index = 0;

   char *line;
   int linelen = 80;
   int len = 0;

   

   /*allocate memory*/
   /*****************/
   linelist = calloc(sizeof(char **), listlen);
   if (linelist == NULL) {
      fprintf(stderr, "%s: Error. Could not allocate memory\n", \
            prog);
      exit(42);
   }

   line = malloc(linelen);
   if (line == NULL) {
      fprintf(stderr, "%s: Error. Could not allocate memory\n", \
            prog);
   }

   /*read input with getline*/
   /*************************/
   /* the former while  fails, lets check if the latter succeeds: well, kinda*/
   /*while (len = getline(&line, &linelen, stdin) && index < linelen -1 )   ) {*/
   while (index < listlen && (len = getline(&line, &linelen, stdin))   ) {
      /* debugging purpose*/   
      printf("size of line is: %d\n", len);
      
      linelist[index] = malloc(len);
      if (linelist[index] == NULL) {
         fprintf(stderr, "%s: Error. Could not allocate memory\n", \
               prog);
      }

      linelist[index] = line;
      /*debugging purpose*/
      printf("line is: %s\n", linelist[index]);
      /*gives correct output*/   
      ++index;
   }

   /*result*/
   /********/
   /* both  gives wrong output,all give the last string entered on stdin*/
   for (index = 0; index < listlen -1; index++) {
      printf("element %d of linelist is: %s\n",  \
         index, linelist[index]);
      }

   printf("%s\n", linelist[0]);
   printf("%s\n", linelist[1]);
   printf("%s\n", linelist[2]);

   return 0;
}

It fails.
What seems to get stored in the **list is three times the last input i get from stdin.
I don't understand why.

Here is the way i get the stdin as a standalone:
Code: Select all
   while (index < listlen && (len = getline(&line, &linelen, stdin))   ) {
      /* debugging purpose*/   
      printf("size of line is: %d\n", len);
      
      linelist[index] = malloc(len);
      if (linelist[index] == NULL) {
         fprintf(stderr, "%s: Error. Could not allocate memory\n", \
               prog);
      }

      linelist[index] = line;
      /*debugging purpose*/
      printf("line is: %s\n", linelist[index]);
      /*gives correct output*/   
      ++index;
   }

printf("line is. %s\n", linelist[index]
gives the right string (the actual stdin). But it doesn't seem to get stored (correct) in "char **linelist".

thanks for a hint.
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am

Re: C, store strings in an array

Postby nadir » May 21st, 2012, 3:27 am

Using
strcpy(linelist[index], line);
instead of
linelist[index] = line;
works. Solved by another user at unix.org.
(Explanation? Thats easy: its beyond me. Yada-memoryaddress-etc-pp)
nadir
 
Posts: 1708
Joined: February 9th, 2011, 8:07 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

x