0

So i'm experimenting with arrays when I come across a bit of a problem

code:

#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char** argv) {
char items[] = {'bread', 'water', 'crisps', 'sweets', 'vegetables'};
for (int i = 0; i < strlen(items); i++) {
        cout << items[i] << endl;
    }
return 0;
}

What's happening is that when the code is ran, it's only outputting the last letter of each item, so 'd' 'r' 's' 's' 's'. I know i'm clearly doing something wrong here but I can't figure out what. I've been surfing on stackoverflow/google for a question like this but clearly what I have done is so obviously wrong, no one has asked!

Any help or a nudge in the right direction to particular documentation would be appreciated!

Thanks.

3

2 Answers 2

1
  1. Need an array of character pointers.
  2. Need to use double quotes
  3. Read a book on C++

i.e. code should be

#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char** argv) {
char *items[] = {"bread", "water", "crisps", "sweets", "vegetables"};
for (int i = 0; i < (sizeof(items) / sizeof(*items)); i++) {
        cout << items[i] << endl;
    }
return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

const char* items[] would be better.
sizeof(items) is a bug.
You see, I've been looking at numerous things and some people were using double quotes and single quotes, so i just went with single (impatient i guess). I've just started learning C++ so i'm still trying to grasp the basics, thanks for the help all and sorry for it being such a naff and easy question!
Also i'm just wondering why (sizeof(items) / sizeof(*items)) works? i understand the pointers part now but i'm still a bit stuck on sizeof, if someone could phrase it in simpleton terms, that'd be great! thanks
@AndrewMatthew: sizeof(items) is the size of the entire array or the size of five pointers to char. sizeof(*items) is the size of the first element of the array. So you have total size divided by the size of a single element to get the number of elements in the array.
|
0

Any of these items in the initializer list

{'bread', 'water', 'crisps', 'sweets', 'vegetables'};

is a multicharacter literal. According to the C++ Standard

A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.

You need to use string literals. I think that what you want is the following

#include <iostream>

int main(int argc, char** argv) 
{
   const char *items[] = { "bread", "water", "crisps", "sweets", "vegetables" };

   for ( const char *s : items ) std::cout << s << std::endl;

   return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.