• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1405
  • Last Modified:

std strlen and integer arrays

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
princess_of_reason
Asked:
princess_of_reason
  • 4
  • 3
1 Solution
 
jkrCommented:
>>    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
 
jkrCommented:
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
 
princess_of_reasonAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
princess_of_reasonAuthor Commented:
The countof thing I don't get. . . it get's the message "invalid initialiser".
0
 
jkrCommented:
>>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
 
jkrCommented:
>>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
 
princess_of_reasonAuthor Commented:
Ah, I see. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now