简体   繁体   中英

Print Pointer Array in C

I would like print the following char Array.

#define LENGTH 8

int main (){

    typedef char Arr[LENGTH];
    Arr test  = {1,0,1};
    Arr* a = &test;
    for(int i=0;i<LENGTH;i++){
        printf("%s ", a[i]);
    }
}

fixing your many errors and re-editing to use strlen:

#define LENGTH 8
#include <stdio.h>
#include <string.h>
int main()
{
    typedef char Arr[LENGTH];
    Arr test  = {'1','0','1'};
    Arr* a = &test;
    size_t len = strlen(test);
    for(int i=0;i<len;i++){
        printf("%c ", (*a)[i]);
    }
}

Three problems:

  1. You are using %s , which is for strings. Use %c for characters.
  2. You need (*a)[i] , as a is a pointer to an array, so *a is an array.
  3. 1 and 0 encode non-printable characters. So you won't see anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM