I am trying to count the characters in a linked list.
I am receiving a compiler error when this function is tested...

**invalid types `char[int]' for array subscript **

int slist::count_c(char c) const
{
   slistelem* temp = h;
   int count = 0; 
   for (int i = 0; i != '\0'; i++)
      if (temp == c[i])
         count ++; 
   return count; 
}

How can I fix this function to properly display desired results?
With a function call such as

cout << l.count_c('b') << endl;

Recommended Answers

All 6 Replies

>> if (temp == c)

Your compiler is correct. Variable c is a single character, not a character array. Maybe you mean if (temp[i] == c)

if( c == temp)

is incorrect as well and gives me this error message:

no match for 'operator==' in '*((+(((unsigned int)(i++)) * 8u)) + temp) == c'

how else can i compare a character to the indexing array?

>>i != '\0'

BAD idea!

how is slistelem declared?

struct slistelem 
{
   char data;
   slistelem* next;
};

Since its a linked list you can not access the nodes in the for loop that you posted, but since you already marked this thread solved I suppose you already figured that out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.