Thread: [Prob] Can't define array String

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Arrow [Prob] Can't define array String

    Hi! I started a week ago with C++, but know already Delphi, various Blitz Dialects, PHP, SDL. So I'm not having that big troubles with C++, but now I have a little Problem: I create a structure with a) the Path to the imagefile and b) the data of the image to load it into the OpenGL Textures. My Code:

    Code:
    struct Struct_Texture {
        public:
        char Path[16];
        GLuint Tex; 
        };
    
    Struct_Texture Texture[20];
    The problem here is C++ related, when I want to declare with

    Code:
    bool PreLoadTextures () {
        i = 0;
        Texture[0].Path = "images/nehe.bmp";
    the images Path I get the Error by the Compiler: "ISO C++ forbids assignment of Arrays". When I use char Path[100]; instead of char Path[16]; I get this: "Incompatible types in Assignment of 'const char[16]' to 'char[100]'" found no help with google. Hope some1 can help me!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Char arrays can only be initialized with a string literal. After a char array has been created, you need to use strcpy() to insert a series of characters into a char array.

    So I'm not having that big troubles with C++
    char arrays as strings are pretty confusing in C++.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Hey Thanks. I meant that I don't have much probs with the C++ Language, this is the first real problem I couldn't solve. And you are right these "strings" in C are just laughable.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you're using C. if you want to use C++, replace all instaces of the following
    Code:
    char <varname>[<size>];
    //with
    std::string <varname>;
    welcome to OO land!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    As Chaos Engine posted, the C++ string type is much easier to deal with. It was created to do away with the problems encountered when using char arrays for strings. Here is an example:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	string str = "hello";
    	str = str + " world";
    	cout<<str<<endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM