The checking for NULL trick only works for NULL terminated strings. For a numeric array you'll have to pass in the size too.

void printArray(int *ptr, size_t length);            
{         
    //for statement to print values using array             
    size_t i = 0;
    for( ; i < length; ++i )      
    printf("%d", ptr[i]);        
}   

 void printString(const char *ptr);            
{         
    //for statement to print values using array             
    for( ; *ptr!='\0'; ++ptr)        
    printf("%c", *ptr);        
}         

int main()    
{    
    int array[6] = {2,4,6,8,10};     
    const char* str = "Hello World!";
    printArray(array, 6);    
    printString(str);
    return 0;     
}
Answer from Praetorian on Stack Overflow
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to input and print array elements using pointers
C program to input and print array elements using pointers - Codeforwin
July 20, 2025 - You can either use (ptr + 1) or ptr++ to point to arr[1]. /** * C program to input and print array elements using pointers */ #include <stdio.h> #define MAX_SIZE 100 // Maximum array size int main() { int arr[MAX_SIZE]; int N, i; int * ptr = ...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_pointers_arrays.php
C Pointers and Arrays
You can also use pointers to access arrays. ... int myNumbers[4] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { printf("%d\n", myNumbers[i]); }
๐ŸŒ
Programming9
programming9.com โ€บ programs โ€บ c-programs โ€บ 169-c-program-to-print-elements-of-array-using-pointers
C Program to Print Elements of Array Using Pointers
C Pointers ยท #include<stdio.h> int main() { int a[5]= {5,4,6,8,9}; int *p=&a[0]; int i; for(i=0; i<5; i++) printf("\nArray[%d] is %d ",i,*(p+i)); for(i=0; i<5; i++) printf("\n %d at %u ",*(p+i),(p+i)); return 0; } Array[0] is 5 Array[1] is 4 Array[2] is 6 Array[3] is 8 Array[4] is 9 5 at 2686708 4 at 2686712 6 at 2686716 8 at 2686720 9 at 2686724 ยท
๐ŸŒ
Technosap
technosap.com โ€บ home โ€บ c programming โ€บ examples โ€บ c program to print elements of array using pointers
C Program to Print Elements of Array using Pointers - Technosap
February 11, 2019 - ... Program to print the elements of array using pointers* #include<stdio.h> main() { int a[5]={5,4,6,8,9}; int *p=&a[0]; int i; clrscr(); for(i=0;i<5;i++) printf("%d ",*(p+i)); getch(); }
๐ŸŒ
Yale University
cs.yale.edu โ€บ homes โ€บ aspnes โ€บ pinewiki โ€บ C(2f)Pointers.html
C/Pointers
So all of a[0], *a, and 0[a] refer to the zeroth entry in a. Unless you are deliberately trying to obfuscate your code, it's best to write what you mean. Because array names act like pointers, they can be passed into functions that expect pointers as their arguments.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-pointers-in-c
Array of Pointers in C - GeeksforGeeks
July 23, 2025 - After that, we can assign a string of any length to these pointers. ... Note: Here, each string will take different amount of space so offset will not be the same and does not follow any particular order. This method of storing strings has the advantage of the traditional array of strings. Consider the following two examples: ... // C Program to print Array of strings without array of pointers #include <stdio.h> int main() { char str[3][10] = { "Geek", "Geeks", "Geekfor" }; printf("String array Elements are:\n"); for (int i = 0; i < 3; i++) { printf("%s\n", str[i]); } return 0; }
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ access-array-pointer
C Program to Access Array Elements Using Pointer
... #include <stdio.h> int main() { int data[5]; printf("Enter elements: "); for (int i = 0; i < 5; ++i) scanf("%d", data + i); printf("You entered: \n"); for (int i = 0; i < 5; ++i) printf("%d\n", *(data + i)); return 0; } ... In this program, the elements are stored in the integer array data[]. ...
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to print array elements using pointer in c - YouTube
We can dereference a pointer using a subscript [] operator to print out the array.
Published ย  March 16, 2024
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2752595 โ€บ how-to-print-value-from-array-using-pointer-to-array
How to print value from array using pointer to array | Sololearn: Learn to code for FREE!
No, we can't By doing int (*ptr)[2] = &arr; We are declaring a single pointer which is meant to point at a data type of " int (*)[2] " which is an integer array of size 2 and making it point to the array "arr" which has to be of size 2 otherwise program will generate error.
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-input-print-array-elements-using-pointers
C program to input and print array elements using pointers | ProCoding
March 6, 2024 - C program to input and print array elements using pointers. Learn step-by-step how to utilize pointers to enhance memory management and optimize array manipulation.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c-program-to-print-array-of-pointers-to-strings-and-their-address
C program to print array of pointers to strings and their address
March 19, 2021 - #include<stdio.h> #include<string.h> void main(){ //Declaring string and pointers, for loop variable// int i; char *a[5]={"One","Two","Three","Four","Five"}; //Printing values within each string location using for loop// printf("The values in every string location are : "); for(i=0;i<5;i++){ printf("%s ",a[i]); } //Printing addresses within each string location using for loop// printf("The address locations of every string values are : "); for(i=0;i<5;i++){ printf("%d ",a[i]); } } When the above program is executed, it produces the following result โˆ’ ยท The values in every string location are: One Two Three Four Five The address locations of every string values are: 4210688 4210692 4210696 4210702 4210707 ยท Letโ€™s consider another example. Stated below is a C program demonstrating the concept of array of pointers to string โˆ’
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_pointer_to_an_array.htm
Pointer to an Array in C
In this code, we have a pointer ptr that points to the address of the first element of an integer array called balance. #include <stdio.h> int main(){ int *ptr; int balance[5] = {1, 2, 3, 4, 5}; ptr = balance; printf("Pointer 'ptr' points to the address: %d", ptr); printf("\nAddress of the first element: %d", balance); printf("\nAddress of the first element: %d", &balance[0]); return 0; }
Top answer
1 of 3
1

in printf("Loop %i: %s\n", i, %data[i]);

you are printing a char that's why %c is required and while printing % is not required

printf("Loop %i: %c\n", i, data[i]);
 #include <stdio.h>
   #include <stdlib.h>
   
   char * askData() {
       static char data[5];
       for(i = 0; i < 5; i++)
           scanf("%s", &data[i]);
   
       return data;
   }
   
   int main() {
       char* data = askData();
       int i; //its better here

       printf("Flag 1\n");
       for(i = 0; i < 5; i++)
           printf("Loop %i: %c\n", i, data[i]);
   
       printf("Flag 2\n");
   }
2 of 3
1

So you're right C doesn't return arrays in the sense that you are talking about, however the way we do that in C is by utilizing two ideas together:

  1. Working with memory: By allocating a region of memory and then returning the starting location of that memory region.

    The function call to allocate memory is called malloc and then you have to use the free call to release that memory region.

  2. Pointer Arithmetic: I'm sure you already know that C has some basic types (char, int, long, float, double, and lately bool); each takes a different amount of memory and so C also provides the convenience of understanding typed pointers (pointers are variables that mark a memory location). A typed pointer understands how "big" it's associated type is in bytes and when you increment its value by 1 in C internally it's incremented by the size of the type.

Combining these ideas we can get arrays:

So for example if you have an array of five integers then you would allocate a region that is 5 * sizeof(int) ; Assuming a modern system the sizeof(int) is going to be 32-bits (or 4 bytes); so you will get a memory region of 20 bytes back and you can fill it up with the values.


#include <stdio.h>
#include <stdlib.h>

int * get_numbers(size_t count) {

     int *data = malloc(count * sizeof(int));
     if (!data) {
         return NULL; // could not allocate memory
     }

     // notice that we are increasing the index i by 1
     // data[i] shifts properly in memory by 4 bytes
     // to store the value of the next input.

     for(int i=0; i < count; i++) {
           scanf("%d", &data[i]);
     }
}

int main() {
     int *data = get_numbers(5);
     if (data == NULL) {
          fprintf(stderr, "Could not allocate memory\n");
          exit(-1);
     }

     for(int i = 0; i < 5; i++) {
         printf("data[%d] = %d\n", i, data[i]);
     }

     free(data); 
}

Now in your case you have a bit of a complication (but not really) you want to be able to read in names, which are "strings" which don't really exist in C except as array of characters.

So to rephrase our problem is that want an array of names. Each name in itself is an array of characters. The thing is, C also understands type pointer pointers (yes a pointer to pointers) and can figure out the pointer arithmetic for that.

So we can have a memory region full of different types; the type pointers themselves can have a list of pointers in a memory region; and then make sure that each pointer in that list is pointing to a valid memory region itself.


#include <stdio.h>
#include <stdlib.h>


char *get_one_name() {
    char *name = NULL;
    size_t len = 0;
    printf("Enter a name:");
    // getline takes a pointer, which if it's set to NULL
    // will perform a malloc internally that's big enough to
    // hold the input value.
    getline(&name, &len, stdin);
    return name;
}

char ** get_names(size_t count) {

    // this time we need memory region enough to hold
    // pointers to names
     char **data = malloc(count * sizeof(char *));
     if (!data) {
         return NULL; // could not allocate memory
     }

     for(int i=0; i < count; i++) {
         data[i] = get_one_name();
     }

     return data;
}

void free_names(char **data, size_t count) {
    for(int i = 0; i < count) {
        if(data[i] != NULL) {
            free(data[i]);
        }
    }
    free(data);
}

int main() {
     char **data = get_names(5);
     if (data == NULL) {
          fprintf(stderr, "Could not allocate memory\n");
          exit(-1);
     }

     for(int i = 0; i < 5; i++) {
         printf("data[%d] = %d\n", i, data[i]);
     }

     free_names(data, 5);
}

Hopefully this gives some ideas to you.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72387178 โ€บ how-to-print-array-elements-using-pointer-in-c
how to print array elements using pointer in c? - Stack Overflow
May 26, 2022 - Reset ptr variable to &arr[0] after first and before second for loop. ... For the first element of the array you don't need to assign with & operator you can directly assign the pointer ` int *ptr = arr;`.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 116116-printing-elements-array-using-pointers.html
printing elements of an array using pointers
May 21, 2009 - #include <stdio.h> static void ... main(void) { double a[5] = { 2.5, 3.2, 18.7, -1.35, 4.2 }; f(a, 5); return 0; } You can use the array subscript operator (that is, []) on pointers....
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 01 โ€บ c-pointer-to-array-example
Pointer and Array in C programming with example
We can also use a pointer variable instead of using the ampersand (&) to get the address. #include <stdio.h> int main( ) { /*Pointer variable*/ int *p; /*Array declaration*/ int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; /* Assigning the address of val[0] the pointer * You can also write like this: * p = var; * because array name represents the address of the first element */ p = &val[0]; for ( int i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %p\n", i, *p, p); /* Incrementing the pointer so that it points to next element * on every increment.
Top answer
1 of 4
12

Given that the code is really simple, I see mostly coding style issues with it.

Instead of this:

char *names[] = {
    "John", "Mona",
    "Lisa", "Frank"
};

I would prefer either of these writing styles:

char *names[] = { "John", "Mona", "Lisa", "Frank" };

// or

char *names[] = {
    "John",
    "Mona",
    "Lisa",
    "Frank"
};

The pNames variable is pointless. You could just use names.

Instead of the while loop, a for loop would be more natural.

This maybe a matter of taste, but I don't think the Hungarian notation like *pArr is great. And in any case you are using this pointer to step over character by character, so "Arr" is hardly a good name. I'd for go for pos instead. Or even just p.

You should declare variables in the smallest scope where they are used. For example *pos would be best declared inside the for loop. In C99 and above, the loop variable can be declared directly in the for statement.

The last return statement is unnecessary. The compiler will insert it automatically and make the main method return with 0 (= success).

Putting it together:

int main(int argc, char *argv[])
{
    char *names[] = { "John", "Mona", "Lisa", "Frank" };
    for (int i = 0; i < 4; ++i) {
        char *pos = names[i];
        while (*pos != '\0') {
            printf("%c\n", *(pos++));
        }
        printf("\n");
    }
} 

Actually it would be more interesting to use argc and argv for something:

int main(int argc, char *argv[])
{
    for (int i = 1; i < argc; ++i) {
        char *pos = argv[i];
        while (*pos != '\0') {
            printf("%c\n", *(pos++));
        }
        printf("\n");
    }
} 
2 of 4
9

Other points not already mentioned:

Use const where possible

It's better to use const where possible so that it's clear when you are intending to modify things and clear when you're not. This helps the compiler help you find bugs early.

Avoid magic numbers

It's better to avoid having numbers like 4 in the program. If you want to change things later, you may well be left wondering "why 4? what does that mean?" Better would be to assign a constant with a meaningful name.

Use putchar rather than printf for single characters

The printf function is very useful, but putchar is often better suited for single-character output. The reason is that printf has to, at runtime, parse the format string, apply the arguments and then emit the results, while putchar only has to pass a character directly to the output. This particular code isn't exactly performance-critical but it's useful to acquire good coding habits from the beginning.

Use C idioms

It's more idiomatic C to have a loop like this for the characters:

while(*pArr) { /* ... */ }

rather than this:

while(*pArr != '\0') { /* ... */ }

Consider using a "sentinel" value for the end of a list

There are two common ways to iterate through a list in C. One is as you already have it, when you know how many values are in the list. The other way is to use a "sentinel" value -- some unique value that tells the program "this is the end of the list." For a list of names such as this program has, a rational choice for a sentinel value would be NULL.

Omit the return 0 from the end of main

Uniquely for main, if the program gets to the end of the function, it automatically does the equivalent of return 0 at the end, so you can (and should) omit it.

Result

Using all of these suggestions, one possible rewrite might look like this:

#include <stdio.h>

int main() 
{
    const char *names[] = { "John", "Mona", "Lisa", "Frank", NULL };

    for (int i=0; names[i]; ++i) {
        const char *ch = names[i]; 
        while(*ch) {
            putchar(*ch++);
            putchar('\n');
        }
        putchar('\n');
    }
}