Copy string
#1

So, from tests that ****** did in the Code Optimization topic it shows that b = a is still the fastest. However, mine doesn't seem to work at all and I have no idea why.

pawn Код:
mPlayerInfo[playerid][mCurrentJob][mCargo] = jobs[playerid][jobid][mCargo];
        printf("%s", mPlayerInfo[playerid][mCurrentJob][mCargo]);
        printf("%s",jobs[playerid][jobid][mCargo]);
Both mCargo s come from the same enum but are used in other places (mPlayerInfo for instance comes from an enum itself, so it saves an enum inside an enum).

the first printf prints "E" and the second printf prints "Equipment". Both strings are the same size, so why does it only take one character?
Reply
#2

Because it assign whatever the first index value in the string array is which is a char. And by default PAWN doesn't have a string datatype. Try using memcpy instead.

https://sampwiki.blast.hk/wiki/Memcpy
Reply
#3

Quote:
Originally Posted by T0pAz
Посмотреть сообщение
Because it assign whatever the first index value in the string array is which is a char. And by default PAWN doesn't have a string datatype. Try using memcpy instead.

https://sampwiki.blast.hk/wiki/Memcpy
But why does it work outside of the enum?

Quote:
Originally Posted by ******
Посмотреть сообщение
The problem here is that you are using data embedded in an enum, which is NOT the same thing.
Can you explain that one a bit more? I got an enum inside an enum, so to say.
Reply
#4

Quote:
Originally Posted by mamorunl
Посмотреть сообщение
But why does it work outside of the enum?
To be honest, I'm not exactly sure. Using memcpy won't make a significant performance changes so I suggest you use that until ****** comes with a solution.


Edit: Enum is more like bunch of const variables stacked up with each other.
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
Because enums are not real arrays, and arrays inside enums are flattened at compile time.
So basically, what I am trying to do here won't work? Normal arrays (or strings even) do work, but when inside another enum it doesn't.
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
Yes. I've tried to explain to people multiple times before that enums aren't arrays, but they don't get it.
Me neither, sorry xD I just use them as easy groups of variables so I can use them as names instead of numbers. Could you give me a way of how this should be done?
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
enums are like defines - in fact they're almost identical:

pawn Код:
#define E_1 0
#define E_2 1
#define E_3 5
#define E_E 6
new arr[E_E];
pawn Код:
enum E_E
{
    E_1,
    E_2[4]
    E_3
}
new arr[E_E];
There are still multidimensional arrays, so why can't I call an array in an array?

pawn Код:
enum mJob {
    mID,
    mCargo[50],
    mTrailer,
    Float:mTrailerX,
    Float:mTrailerY,
    Float:mTrailerZ,
    Float:mTrailerA,
    mCompanyFrom,
    mCompanyTo,
    mValue
};
pawn Код:
enum playerInfo {
    mCurrentJob[mJob]
} // all other variables omitted for the sake of clarity
Why doesn't this work?

I get your defines way, but I can still redefine everything, even parts of an array.

Edit: OK, so I tried to put it outside the enum and make it like any other enum. I replaced my code with this:
pawn Код:
currentJob[playerid][mCargo] = jobs[playerid][jobid][mCargo];
        printf("%s", currentJob[playerid][mCargo]);
        printf("%s",jobs[playerid][jobid][mCargo]);
But it still only prints 1 character, whilst the second calling works just fine. I really don't understand why.

I tried to copy it with memcpy, as suggested as well, but all that did was give me an empty string (just like strmid btw)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)