English 中文(简体)
不了解点人与++和__
原标题:Not Understanding Pointer Arithmetic with ++ and --
最佳回答

其中,++/code>的位次位次优于*,从而使点名上加一,但检索原地点的价值,因为加固后点名人返回,然后加之其价值。

<代码>(*p>++>

这迫使位于另一个方向,因此,点人首先被忽略,然后该地点的价值被一加一(但原点地点的价值被退回)。

<代码>*(p++>

这一点首先增加了点名,使之与第一点相同。

值得注意的是,点名增加的数额受到点名类型的影响。 根据你提供的联系:

char *mychar;
short *myshort;
long *mylong;

<代码>char是一个按期编号的编码,++>将点码提高1(因为点人指每个星体的开端)。

<代码>short为两条长字,++>将点击器增加2个,以便在下一个短程开始时而不是在下台开始。

<代码>long为4英字母,++>将点人增加4。

问题回答

几年前,我从克里尼加汉/里奇里(现在我没有案文,希望其准确的法典):py_0, cpy_1, cpy_2等同str:

char *cpy_0(char *t, const char *s)
{
    int i = 0;
    for ( ; t[i]; i++)
        t[i] = s[i];
    t[i] = s[i];
    i++;
    return t + i;
}
char *cpy_1(char *t, const char *s)
{
    for ( ; *s; ++s, ++t)
        *t = *s;
    *t = *s;
    ++t;
    return t;
}
char *cpy_2(char *t, const char *s)
{
    while (*t++ = *s++)
        ;
    return t;
}

First you have to understand what post increment does;
The post increment, increases the variable by one BUT the expression (p++) returns the original value of the variable to be used in the rest of the expression.

char   data[] = "AX12";
char* p;

p = data;
char* a = p++;

// a ->  A   (the original value of p was returned from p++ and assigned to a)
// p ->  X 

p = data;   // reset;
char  l =  *(p++);

// l =   A . The (p++) increments the value of p. But returns the original
             value to be used in the remaining expression. Thus it is the
             original value that gets de-referenced by * so makeing l  A 
// p ->  X 

如今,由于运营商优先:

*p++ is equivalent to *(p++)

最后,我们面临复杂的问题:

p = data;
char m = (*p)++;

// m is  A . First we deference  p  which gives us a reference to  A 
//           Then we apply the post increment which applies to the value  A  and makes it a  B 
//           But we return the original value ( A ) to be used in assignment to  m 

// Note 1:   The increment was done on the original array
//           data[]  is now "BXYZ";
// Note 2:   Because it was the value that was post incremented p is unchaged.
// p ->  B  (Not  X )
*p++

归还内容*p,从而提高点人的价值(后加)。 例如:

int numbers[2];
int *p;
p = &numbers[0];
*p = 4;        //numbers[0] = 4;
*(p + 1) = 8;  //numbers[1] = 8;
int a = *p++;  //a = 4 (the increment takes place after the evaluation)
               //*++p would have returned 8 (a = 8)
int b = *p;    //b = 8 (p is now pointing to the next integer, not the initial one)

以及:

(*p)++

它提高了内容的价值,*p = *p + 1;

(p++); //same as p++

增加点数,以说明你宣布点名时界定的规模的下一个要素(可能不存在)。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签