开发者

Question about a pointer in a program. [C]

Here is the full code of it

#include <stdio.h>
#include <string.h>

void reverse_string(unsigned short *buf, int length)
{
    int i;
    unsigned short temp;

    for (i = 0; i < length / 2; i++)
    {
        temp = buf[i];
        buf[i] = buf[length - i - 1];
        buf[length - i - 1] = temp;
    }   
}

int main(int argc, char **argv)
{
    unsigned short* tmp = (unsigned short*)argv[1];
    reverse_string(tmp,strlen(argv[1]) / 2);

    printf("%s",argv[1]);
开发者_如何学编程
    return 0;   
}

As you can see, in main, we have

unsigned short* tmp = (unsigned short*)argv[1];

Arent pointers supposed to point "to the address of" of a variable? The one above isn't(using the ampersand). Yet the program works as intended.

Why is it like that?

And what does this part mean?

(unsigned short*)argv[1]


argv is a pointer-to-an-array-of-pointers:

  • argv[0][0] (a char)
  • argv[0] (a char*)
  • argv (a char**)

    unsigned char* tmp = (unsigned char*)argv[1];

...works, because you're referencing the the second "string" in that set.

Note that in this case, "char" and "unsigned short" might be roughly equivolent depending on the compiler and platform, but it is probably not a good idea to assume that. For example, if you compiled to enable a "unicode" command line, then you might get "short" instead of "char" forwarded to you from the command line. But, that may be a dangerous assumption, as "these days" a "short" is usually 16-bits and a "char" is usually 8-bits.


Addressing the original questions:

argv is an array of pointers, each of which point to a character array. argv[1] is a pointer to the character array with the first argument (i.e. if you run ./program arg1 arg2, the pointer argv[1] points to the string arg1).

The ampersand is used to denote a reference, which is for most purposes the same as a pointer. It is syntactic sugar to make it easy to pass a reference to a variable that you have already declared. The common example is using scanf.

int x = 1;
scanf(..., &x, ...)

is equivalent to

int x = 1;
int *p = &x;
scanf(..., p, ...)

The program itself is designed to flip endianness. It's not sufficient to go character-by-character because you have to flip two bytes at a time (ie short-by-short), which is why it works using shorts.

(unsigned short*)argv[1] instructs the compiler to treat the address as if it were an array of shorts. To give an example:

unsigned char *c = (unsigned char *)argv[1];
c[1]; /*this points to the address one byte after argv*/
unsigned short *s = (unsigned short *)argv[1];
s[1]; /*this points to the address two bytes after argv */


Take a look at a primer on type casting.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜