Traversing in Array of pointers


 
Thread Tools Search this Thread
Top Forums Programming Traversing in Array of pointers
# 1  
Old 06-09-2010
Power Traversing in Array of pointers

Please find the below program. the requirement and description of the program also given:


Code:
ganesh@ubuntu:~/my_programs/c/letusc/chap9$ cat fa.c.old
/* Program : write a program to count the number of 'e' in thefollowing array of pointers to strings:

	char *s[] = {
			"We will teach you how to..",
			"Move a mountain",
			"Level a building",
			"Erase the past",
			"Make a million",
			"....all through C!"
		    };
*/

/* Date : 09-June-2010 */

# include <stdio.h>
# include <string.h>

int main(void)
{
        char *s[] = {
                        "We will teach you how to..",
                        "Move a mountain",
                        "Level a building",
                        "Erase the past",
                        "Make a million",
                        "....all through C!"
                    };
	int count=0;
	char *sptr;
	sptr=*s;

		while ( *sptr!='\0' ) {
			if (*sptr=='e') { ++count;}
	        	++(sptr);
		}
	printf("count of e's : %d\n", count); 
	return 0;
}

Output of the above program:

Code:
ganesh@ubuntu:~/my_programs/c/letusc/chap9$ gcc -o fa fa.c
ganesh@ubuntu:~/my_programs/c/letusc/chap9$ ./fa
count of e's : 2

It only counts the e's in first line. I know I have to traverse through rest of*s[]. But don't know how to do it. Can any one please help me in this?

Thanks in Advance,
Ramkrix

Last edited by Scott; 06-14-2010 at 09:23 AM.. Reason: Changed font=courier to code tags
# 2  
Old 06-09-2010
Code:
int n;

for(n=0; n<5; n++)
{
  char *str=s[n];
  // count all e's in s
  ...
}

# 3  
Old 06-09-2010
Bug

Thanks for the quick reply. I wll use this. Howevr, I have one more question. Here from the declaration of char *s[], we are easily able to note that there 6 "pointer to char arrays" and using a variable as suggested by you, we can traverse. Is there any other way, such that I am not using any variable line "n" to traverse.

I am asking like this:
we have '\0' character to say "end of string" and we use this character to traverse along string like (*sptr!='\0')
Similarly, do we have any thing to notify "end of pointer to character arrays" in Array of pointers.

Thanks,
Ramkrix
# 4  
Old 06-09-2010
Quote:
Originally Posted by ramkrix
Thanks for the quick reply. I wll use this. Howevr, I have one more question. Here from the declaration of char *s[], we are easily able to note that there 6 "pointer to char arrays" and using a variable as suggested by you, we can traverse. Is there any other way, such that I am not using any variable line "n" to traverse.

I am asking like this:
we have '\0' character to say "end of string" and we use this character to traverse along string like (*sptr!='\0')
Similarly, do we have any thing to notify "end of pointer to character arrays" in Array of pointers.

Thanks,
Ramkrix
You need another char** that points to s...and ensure that the array of char pointers s is null terminated.
# 5  
Old 06-09-2010
Quote:
Originally Posted by ramkrix
Thanks for the quick reply. I wll use this. Howevr, I have one more question. Here from the declaration of char *s[], we are easily able to note that there 6 "pointer to char arrays" and using a variable as suggested by you, we can traverse. Is there any other way, such that I am not using any variable line "n" to traverse.

I am asking like this:
we have '\0' character to say "end of string" and we use this character to traverse along string like (*sptr!='\0')
Similarly, do we have any thing to notify "end of pointer to character arrays" in Array of pointers.
Strings NULL-terminate themselves, but nothing else does, so your array is not NULL-terminated; it couldn't know when to stop. And each string is fully independent, the contents of one string won't help you find where the next begins.

If the array was NULL-terminated, you could do so like this:

Code:
# include <stdio.h>
# include <string.h>

int main(void)
{
char *s[] = {
"We will teach you how to..",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"....all through C!",
NULL /* Needed to know where to stop! */
};

char **ptr=s;

while( (*ptr) != NULL)
{
  char *str=(*ptr);

  // count e's in str
  ...

  // move to next string
  ptr++;
}

}

# 6  
Old 06-09-2010
Thank you Corona and Shamrock
# 7  
Old 06-14-2010
homework?
you don't need a NULL pointer, though it's one method.
well I can tell you the idiom to correctly go through a char*[]...

Code:
# include <stdio.h>
# include <string.h>

int main(void)
{
    char *s[] = {
    "We will teach you how to..",
    "Move a mountain",
    "Level a building",
    "Erase the past",
    "Make a million",
    "....all through C!"
    };

    size_t n = sizeof s/sizeof(char *);
    char **p = s;

    while (n--) {
       puts(*p++);
       // create a counting function here
   }
}

Code:
$ ./ramk
.$ ./ramk
We will teach you how to..
Move a mountain
Level a building
Erase the past
Make a million
....all through C!
$


Last edited by bigearsbilly; 06-14-2010 at 06:21 AM.. Reason: oops!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (3 Replies)
Discussion started by: spflanze
3 Replies

2. Programming

Traversing member of structure of vector C++

Hello, I want to loop thru a vector composed of many entries as structure, which contains sequenceID and sequence. At looping, delete any structure if the sequence is a perfect-match substring of another sequence of any other structure, so that the resulted vector contains only unique sequences.... (1 Reply)
Discussion started by: yifangt
1 Replies

3. Programming

Pointers and array

Hello, I read from a book exercise for a challenge. How to print out each letter of char array a by two different pointers pa and ppa in the example? I have tried my code for letter "r" by testing without full understanding as only the first one worked. #include<stdio.h> int main() { char... (17 Replies)
Discussion started by: yifangt
17 Replies

4. Shell Programming and Scripting

traversing a string

I am writing a script which will read a word and say how many vowels and consonants does the word contain. but i dont know how to traverse a string in shell scripting. if it was in C i'd have done something like this: cout<<"plz enter the word"<<endl; cin>>word; int consonants, vowels;... (4 Replies)
Discussion started by: nishrestha
4 Replies

5. Homework & Coursework Questions

Problem while traversing directories

I was given to create a backup of all files in a given directory(command line argument) into say /home/vishal/back and the back up files must be accordingly to the extension of the file i.e pdf files are saved in back/pdf doc files back/doc etc . I gave a recursive function to traverse through the... (1 Reply)
Discussion started by: davis7son
1 Replies

6. Programming

Problem with array of pointers

Hi All, I am using the array of pointers and storing the address of string.This is a global list. So i am using extern to give the reference of this list to another file and using reading the data from this string. But list is being corrupted and string is missing some characters in... (2 Replies)
Discussion started by: lovevijay03
2 Replies

7. UNIX for Dummies Questions & Answers

script for traversing directory

hi please suggest a korn script which will traverse all subdirectory in the current directory? (2 Replies)
Discussion started by: ilayans
2 Replies

8. Programming

Vector Traversing

Hi i have the following structure struct S { char Mod_num; char val; char chr_nm_cd; } I am reading a 2GB file and inserting into the structure and writing into a vector. I feel like only vector will be a right option. I tried with multimap but it is memory intensive and hence i... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

9. UNIX for Dummies Questions & Answers

Traversing a file system

I'm pretty new at this UNIX stuff, and this may be a simple question but I'm kind of stuck :confused: Let's say I have a large directory structure of .essay files, where I saved all of the essays that I did over the last few years. Not all of the .essay files are in the same directory (all... (1 Reply)
Discussion started by: hooj
1 Replies

10. Programming

Pointers and array

hi all, let say i have a pointer exit, and this exit will store some value. how can i store the value that the pointer points to into an array and then print them out from the array. thanks in advance (2 Replies)
Discussion started by: dianazheng
2 Replies
Login or Register to Ask a Question