C指针数组问题

时间:2019-05-19 12:28:04

标签: c arrays pointers printing

我现在正在处理一些c指针,但是我遇到的问题是,参数数组中的某些指针确实在第二个参数上打印出了错误,或者实际上甚至指向了错误的地址。这是怎么回事?

Argument 1 is named : test and has the address of : 0x7ffe563f45e3
Argument 2 is named : test123 and has the address of : 0x7ffe563f45e8
Argument 3 is named : hmm and has the address of : 0x7ffe563f45f0
Argument 4 is named : weird and has the address of : 0x7ffe563f45f4

ArgumentPointer 1 is named : test and has the address of : 0x7ffe563f45e3
ArgumentPointer 2 is named :��y�U and has the address of : 0x7ffe563f3c08
ArgumentPointer 3 is named : hmm and has the address of : 0x7ffe563f45f0
ArgumentPointer 4 is named : weird and has the address of : 0x7ffe563f45f4

代码在这里:

#include <stdio.h>

int main(int argc, char*argv[])
{
char *arguments[] = { "" };

if (argc == 1)
{
    printf("There are no Arguments given!\n\n");
}

for (int i = 1; i < argc; i++)
{
    arguments[i] = argv[i]; 
        printf("Argument %d is named : %s and has the address of : %p\n",i
, arguments[i], 
            arguments[i]);
}

printf("\n");

char **argumentPointer = arguments;
for (int i = 1; i < argc; i++)
{
    printf("ArgumentPointer %d is named : %s and has the address of :    
%p\n", i, argumentPointer[i],
            argumentPointer[i]);
}

}

1 个答案:

答案 0 :(得分:-1)

哎呀...只是忘了初始化我的Array的大小!

我的坏人!