Thread: help understanding arrays of strings!

  1. #16
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Looks pretty. Nice job. A small remark, you are not required but it is generally good practice to include the names of the function parameters at the function prototypes, as they tend to improve readability, and document the role of every parameter in the function.
    E.g. taken from stdio.h
    Code:
    char *strcpy(char *dest, char *src);
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also it is a good practice to put with the array - the array length as a second parameter
    in this case your printStrings function does not need to know that arrays are declared with STRING_COUNT constant

    also when fgets returns null it could be not due to error but due to EOF condition, so maybe user just does not want to enter all 5 srings and will stop after 3 (he can emulate EOF pressing Ctrl+Z if I remember)
    so in this case you also may want to proceed the actual number of received strings, and not the preallocated number - so passing the number of filled array members will also help to solve this problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by vart View Post
    also it is a good practice to put with the array - the array length as a second parameter
    in this case your printStrings function does not need to know that arrays are declared with STRING_COUNT constant

    also when fgets returns null it could be not due to error but due to EOF condition, so maybe user just does not want to enter all 5 srings and will stop after 3 (he can emulate EOF pressing Ctrl+Z if I remember)
    so in this case you also may want to proceed the actual number of received strings, and not the preallocated number - so passing the number of filled array members will also help to solve this problem
    hi!
    these are very important considerations so I tried to put them together in this other small program which shows and counts the strings which terminate by a key string entered by the user.
    The problem is that i cannot catch the EOF condition properly (this is the reason why i entered the blank line request): if i press CTRL+Z i get just a "stopped" message and the program gets terminated... so the while cicle in my readlines() never fails (and i have to check in the block for blank lines!)
    do you have any hints? is it possible to declare the key string in a more elegant way than the way i did? thanks again!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAXLINES 5
    #define MAX_STRLEN 256
    
    int readlines (char * lines[], int maxlines);
    int countlines (char * lines[], int numlines, char * key,int keyDim);
    void writelines (char * lines[],int numlines);
    
    int main()
    {
    	char *lines[MAXLINES], *key[1];
    	printf("Insert text. Leave a blank line when you're finished\n");
    	fflush(stdout);
    	int numl;
    
    	if ((numl=readlines(lines,MAXLINES))<0) 
    	{
    		exit(1);
    	}
    
    	printf("Enter your key string\n");
    	fflush(stdout);
    
    	if (readlines(key,1)<0)
    	{
    		exit(1);
    	}
    
    	printf("Your entered lines (%d total):\n",numl); 
    	writelines(lines,numl);
    	printf("Your key:\n");
    	writelines(key,1);
    	printf("String(s) which terminate by your key:\n");
    	printf("I found %d such line(s)\n",countlines (lines,numl,key[0],strlen(key[0])));
    }
    
    
    int readlines (char * lines[],int maxlines)
    {
    	char buffer[MAX_STRLEN];
    	int line=1;
    	while ( fgets(buffer,MAX_STRLEN,stdin) ) 
    	{
    		if (strlen(buffer)==1) return line-1;
    
    		buffer[strlen(buffer)-1]='\0';
    		if (! (lines[line-1]=malloc(strlen(buffer)+1)) )
    		{
    			printf("Out of memory\n");
    			return -1;
    		}
    		strcpy(lines[line-1],buffer);
    		
    		if (line==maxlines) return line;
    
    		line++;
    		
    	}
    	return line-1;
    }
    
    int countlines (char * lines[], int numlines, char * key,int keyDim)
    {
    	int i,count=0;
    	for(i=0;i<numlines;i++)
    	{
    		if ( strstr((lines[i]+(strlen(lines[i])-keyDim)),key) ) {
    		count++;
    		puts(lines[i]);
    		}
    	}
    	return count;
    }
    
    void writelines (char * lines[],int numlines)
    {
    	int i;
    	for (i=0;i<numlines;i++) 
    	{
    		puts(lines[i]);
    	}
    }

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you are missing freeing of all allocated memory before exit
    2. in your countlines function strcmp is more suitable when strstr
    3. strlen returns size_t - you should update the countlines function prototype accordingly
    4. you are using var declarations mixed with other statements - it is C99, C89 does not allow this
    5. here is my output when using Ctrl+Z to emulate EOF
    Code:
    Insert text. Leave a blank line when you're finished
    abs
    ^Z
    Enter your key string
    123
    Your entered lines (1 total):
    abs
    Your key:
    123
    String(s) which terminate by your key:
    I found 0 such line(s)
    Press any key to continue . . .
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help understanding arrays and pointers
    By flicka in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 09:45 PM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM