开发者

Newbie question on C++ array

开发者 https://www.devze.com 2023-03-08 11:07 出处:网络
I\'m working through a demo program written by someone else, and I\'m very confused about some of the statements they are using. I\'m not very familiar with C++ (moreso with Obj-C) and I\'m not sure i

I'm working through a demo program written by someone else, and I'm very confused about some of the statements they are using. I'm not very familiar with C++ (moreso with Obj-C) and I'm not sure if this is valid code or not. Take, for example, the following: (comments are mine)

int main(int argv, char** argc)
{
    int perm [20]; //OK, so declare an array of ints, size = 20
    for (int i=0; i < 20; i++)
        perm = i; //whaaaaa??? thought you need to specify an element to assign to an array...
}

That is one example - my compiler throws an "incompatible types in assig开发者_StackOverflow中文版nment of 'int' to 'int [20]' error, but apparently others have been able to compile the program. Am I nuts, or is this bad code?

Here's another piece I just don't get:

int d[20] = {0}; //OK, another int array of size 20, initialized to 0's
for (int i = 1; i < n; i++)
{
    d = d[i - 1]; //this I don't get - assign the array to one of its own elements??
    if (invperm[i - 1] < b)
        d++;  //this would just increment to the next element? 
}

I suspect the error is one of comprehension on my part, as if the code was bad other people would've commented on that fact...if anyone has a good explanation and/or resource I can read to understand this, I would be most appreciative!

Thanks!

*EDITED TO ADD*

In response to the answers below, I did copy/paste that code, and it looks intact to me...I can only assume when the original author posted it, it mangled it somehow. Thanks for the replies, I'm glad I had the right understanding, and I'll try and contact the author to see if there is an un-mangled copy out there somewhere!


All those examples are absolutely wrong. It looks like you lost [i] when you copied the code from wherever you got it from.

I have seen something similar with code sent over messenger programs that treat certain bits of text as emotes and replace them with images that don't get copied as text, but instead get dropped.


Your understanding is fine, that code is just entirely nonsensical.

d++;  //this would just increment to the next element?

It would if d were a pointer. However since d is an array, it's simply illegal.


This is most certainly a copy/paste error. I have succumbed to the temptation of copy/pasting code at one point during a game tech project involving Lua scripts. If the Lua script fails there is no feedback/output that indicates something is failed (which is very frustrating). After debugging for hours I realised my script was using 'smart quotes'.

Whilst this code is broken it can still teach you some things about C++.

int perm [20];

cout << endl << perm << endl;
cout << endl << &perm[0] << endl;

'perm' returns the memory address of the first element of the array. so when you are trying to assign 'i' to 'perm' in that main for loop (20 times) you will know now that you were trying to assign an integer to a memory address, hence the incompatible type error.

The second section however is verry broken and I can't discern much learning from this :P.

I added in an example program to show how pointers/arrays can be used:

#include <iostream>

using namespace std;

int main()
{
    int d[20] = {0}; // 20 ints, set to 0
    int * ptr = d; // points to d[0]'s memory address

    for(int i = 0; i < 20; i++)
    {
        d[i] = 0 + i; // set array values
    }

    for(int i = 0; i < 20; i++)
        {    
                // iterates through d and prints each int
        cout << endl << "d[i]: " << d[i] << endl;
                // dereferences the ptr to get the same int
                // then incraments the position of the pointer for next time
        cout << endl << "*ptr++: " << *ptr++ << endl;
    }

    getchar();

    return(0);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消