Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

std strlen and integer arrays

Posted on 2005-05-13
7
Medium Priority
?
1,399 Views
Last Modified: 2013-12-14
Can you use the strlen() function on an integer array?

I was playing around with it, and while

///
    char string[50]= "spookypurpledragon";
    cout << "Length of spookypurpledragon is: " << strlen(str) << endl;
///

. . . works, this:

///
    int string[50]= "spookypurpledragon";
    cout << "Length of spookypurpledragon is: " << strlen(str) << endl;
///

. . . doesn't. That makes perfect sense to me, but I've been given an assignment in my programming class where part of the code I'm expected to use has a function where strlen is used on an int array[]. . . . which won't compile in Dev-C++. I'm tempted to say the instructor made a mistake, but he's given this assignment out before apparently so if that was the problem he'd have caught it.

So can you?  ( Use strlen on an integer array that is. )
0
Comment
Question by:princess_of_reason
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13996877
>>    int string[50]= "spookypurpledragon";

This is not a null terminated string, and you should in fact get compiler warnings/errors about the assignment. That's why it does not work.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13996902
BTW, are you thinking about

#define COUNTOF(a)           (sizeof(a)/sizeof(a[0]))

   int string[50]= "spookypurpledragon"; // even though this makes *no* sense
   cout << "Length of spookypurpledragon is: " << COUNTOF(str) << endl;
0
 

Author Comment

by:princess_of_reason
ID: 13996974
Aha . . . so how would I include the null character at the end ? I just tried adding \0 to the end . . . which didn't work.

The errors I get are :

>>invalid initialisation
>>cannot convert `int*' to `const char*' for argument `1' to `size_t strlen(const char*)'
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:princess_of_reason
ID: 13996997
The countof thing I don't get. . . it get's the message "invalid initialiser".
0
 
LVL 86

Expert Comment

by:jkr
ID: 13997012
>>so how would I include the null character at the end ?

You would not do that at all - 'strlen()' is for null terminated strings, an array of integers is something completely different. The 'COUNTOF()' mentioned above is probably closest to what you are trying to achieve.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13997068
>>it get's the message "invalid initialiser"

Yes, that's because

int string[50]= "spookypurpledragon";

simply is incorrect. What would work is

#include <iostream>
using namespace std;

#define COUNTOF(a)           (sizeof(a)/sizeof(a[0]))

void main () {

  int spookypurpledragon[] = { 1,2,3,4,5,6,7,8,9};
  cout << "Length of spookypurpledragon is: " << COUNTOF(spookypurpledragon) << endl;
}

Output:

Length of spookypurpledragon is: 9
0
 

Author Comment

by:princess_of_reason
ID: 13997112
Ah, I see. Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question