指针数组和数组指针实例

1.#include

typedef int(AINT5)[5];
typedef float(AFLOAT10)[10];
typedef char(ACHAR9)[9];

int main()
{
    AINT5 a1;
    float fArray[10];
    AFLOAT10* pf = &fArray;
    ACHAR9 cArray;
    char(*pc)[9] = &cArray;
    char(*pcw)[4] = cArray;
    
    int i = 0;
    
    printf("%d, %d\n", sizeof(AINT5), sizeof(a1));
    
    for(i=0; i<10; i++)
    {
        (*pf)[i] = i;
    }
    
    for(i=0; i<10; i++)
    {
        printf("%f\n", fArray[i]);
    }
    
    printf("%0X, %0X, %0X\n", &cArray, pc+1, pcw+1);
}

2.#include
#include

int lookup_keyword(const char* key, const char* table[], const int size)
{
    int ret = -1;
    
    int i = 0;
    
    for(i=0; i    {
        if( strcmp(key, table[i]) == 0 )
        {
            ret = i;
            break;
        }
    }
    
    return ret;
}

#define DIM(a) (sizeof(a)/sizeof(*a))

int main()
{
    const char* keyword[] = {
            "do",
            "for",
            "if",
            "register",
            "return",
            "switch",
            "while",
            "case",
            "static"
    };
    
    printf("%d\n", lookup_keyword("return", keyword, DIM(keyword)));
    printf("%d\n", lookup_keyword("main", keyword, DIM(keyword)));
}

3.#include

int main(int argc, char* argv[], char* env[])
{
    int i = 0;
    
    printf("============== Begin argv ==============\n");
    
    for(i=0; i    {
        printf("%s\n", argv[i]);
    }
    
    printf("============== End argv ==============\n");
    
    printf("\n");
    printf("\n");
    printf("\n");
    
    printf("============== Begin env ==============\n");
    
    for(i=0; env[i]!=NULL; i++)
    {
        printf("%s\n", env[i]);
    }
    
    printf("============== End env ==============\n");
}
指针数组和数组指针实例_第1张图片

 

转载于:https://www.cnblogs.com/wxb20/p/6150569.html

你可能感兴趣的:(指针数组和数组指针实例)