5 Replies - 2410 Views - Last Post: 01 November 2009 - 10:40 AM Rate Topic: -----

#1 Autocrat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 307
  • Joined: 27-September 09

Determine end of variable char array

Posted 01 November 2009 - 12:04 AM

How can we iterate a variable sized char array? Suppose the maximum size of the array is 64, but the user input is always far less than that, how do we find out when the last element has been dealt with?
Is This A Good Question/Topic? 0
  • +

Replies To: Determine end of variable char array

#2 Spencer William  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 14-June 09

Re: Determine end of variable char array

Posted 01 November 2009 - 12:10 AM

Well, I think I know what you are getting at. Unless you are taking a class that is requiring you to use a standard array, use a vector. They automatically resize and you can get the last element by simply setting up an iterator and getting the last element. Like this.

vector<char> MyVector;
vector<char>::iterator pos;

//Load values into the vector...

//Then retrieve what you want.
//this gives you the last non-null element
pos = MyVector.end() - 1;



It is a little more complicated using a standard array, but this should work without having to use a while loop.


//This gives you the amount of elements in the array
int elements = sizeof(YourArray)/sizeof(int) //for sizeof(int), replace int with whatever the type of your array is. 

//Now that you know how how many elements are in your array you can find the last element like this. 
cout << array[elements - 1] //subtract one because the array is 0 to N-1 not 1 to N



If you have some code, feel free to post it.

I hope this helps! :)

This post has been edited by Spencer William: 01 November 2009 - 12:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Spencer William  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 14-June 09

Re: Determine end of variable char array

Posted 01 November 2009 - 12:24 AM

Whoops...I forgot the title of this post said it was a "char array". In that case replace sizeof(int) with sizeof(char). :)
Was This Post Helpful? 0
  • +
  • -

#4 gronk  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 168
  • Joined: 28-October 09

Re: Determine end of variable char array

Posted 01 November 2009 - 04:59 AM

View PostAutocrat, on 31 Oct, 2009 - 11:04 PM, said:

How can we iterate a variable sized char array? Suppose the maximum size of the array is 64, but the user input is always far less than that, how do we find out when the last element has been dealt with?


Depends what you mean by "variable sized char array". Is it something like this, with and example of how to get user input:
char str[64];
fgets(str,60,stdin);


If so then fgets will add a \n and \0 at the end of the input, and you can check for this:
for (int i=0; str[i]; i++)
{
  if (isprint(str[i]))
	printf("You entered the following character: '%c'\n",str[i]);
}


So the input "Hello" will (should) result in the following output:
You entered the following character: 'H'
You entered the following character: 'e'
You entered the following character: 'l'
You entered the following character: 'l'
You entered the following character: 'o'

This post has been edited by gronk: 01 November 2009 - 05:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: Determine end of variable char array

Posted 01 November 2009 - 10:18 AM

View PostAutocrat, on 1 Nov, 2009 - 08:04 AM, said:

How can we iterate a variable sized char array? Suppose the maximum size of the array is 64, but the user input is always far less than that, how do we find out when the last element has been dealt with?


If C string strlen().
Was This Post Helpful? 0
  • +
  • -

#6 Autocrat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 307
  • Joined: 27-September 09

Re: Determine end of variable char array

Posted 01 November 2009 - 10:40 AM

By gronk is the best answer. Thanks to all of you though.

@Spencer William, thanks but I don't do vectors at the moment :)
@#define, sorry but I forgot to mention I am using C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1