Sponsored Content
Top Forums Programming Traversing in Array of pointers Post 302428314 by Corona688 on Wednesday 9th of June 2010 12:51:53 PM
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++;
}

}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRSTR(3)						   BSD Library Functions Manual 						 STRSTR(3)

NAME
strcasestr, strcasestr_l, strnstr, strstr -- locate a substring in a string LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * strcasestr(const char *s1, const char *s2); char * strnstr(const char *s1, const char *s2, size_t n); char * strstr(const char *s1, const char *s2); #include <string.h> #include <xlocale.h> char * strcasestr_l(const char *s1, const char *s2, locale_t loc); DESCRIPTION
The strstr() function locates the first occurrence of the null-terminated string s2 in the null-terminated string s1. The strcasestr() function is similar to strstr(), but ignores the case of both strings. The strnstr() function locates the first occurrence of the null-terminated string s2 in the string s1, where not more than n characters are searched. Characters that appear after a '' character are not searched. Since the strnstr() function is a FreeBSD specific API, it should only be used when portability is not a concern. While the strcasestr() function uses the current locale, the strcasestr_l() function may be passed a locale directly. See xlocale(3) for more information. RETURN VALUES
If s2 is an empty string, s1 is returned; if s2 occurs nowhere in s1, NULL is returned; otherwise a pointer to the first character of the first occurrence of s2 is returned. EXAMPLES
The following sets the pointer ptr to the "Bar Baz" portion of largestring: const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strstr(largestring, smallstring); The following sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched: const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strnstr(largestring, smallstring, 4); SEE ALSO
memchr(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strtok(3), wcsstr(3), xlocale(3) STANDARDS
The strstr() function conforms to ISO/IEC 9899:1990 (``ISO C90''). BSD
October 11, 2001 BSD
All times are GMT -4. The time now is 09:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy