Sponsored Content
Top Forums Programming C; storing strings in an array Post 302643819 by tornow on Sunday 20th of May 2012 06:44:46 PM
Old 05-20-2012
C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array.


If i do this:
using a char **list to store strings
allocate memory to it
Code:
#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:
#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:
    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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

3. Shell Programming and Scripting

storing records in awk array

hi i have a file as follows: 1 2 3 4 5 6 i want to store all these numbers in an array using awk.. so far i have: awk '{for(i=1;i<=NR;i++) {a=$1}} END {for(i=1;i<=NR;i++) {printf("%1.11f",a)}}' 1.csv > test however, i am getting all values as zero in the "test" file..... (3 Replies)
Discussion started by: npatwardhan
3 Replies

4. Shell Programming and Scripting

storing values in a list or array

i have a file called file.txt having the following entries. 2321 2311 2313 4213 i wnat to store these values in a list and i want to iterate the list using loop and store it in another list (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

5. Shell Programming and Scripting

Storing blanks in an array in bash

Hi Guys, I have a file which is as follows: 1 2 4 6 7 I am trying to store these values in an array in bash. I have the following script: FILE=try.txt ARRAY=(`awk '{print}' $FILE`) echo ${ARRAY} (3 Replies)
Discussion started by: npatwardhan
3 Replies

6. UNIX for Dummies Questions & Answers

storing strings in C

i'm trying to figure out how to store strings into an array, can someone help me figure it out? i would like to store the following for example: array = "dog" array = "cat" the inputs are coming from the command line. Thanks, (6 Replies)
Discussion started by: l flipboi l
6 Replies

7. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

8. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies

9. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

10. UNIX for Beginners Questions & Answers

Storing timestamp of files in an array

Hi, I have written the below script to get the timestamp of each files and result is as below Script find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'} Input -rw-r--r-- 1 user domain users 17382 Jul 19 06:10... (5 Replies)
Discussion started by: nextStep
5 Replies
All times are GMT -4. The time now is 11:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy