You are here:

C/Doubt in topic pointers

Advertisement


Question
sir,
What is the implication of declaration-
"char *a[20]"?
Is it a 2-d array?Then why?
Or is it an array of pointers?
Kindly explain...

Answer
Hi Akash,

This is a 1d array of char pointers. Please see the following example program.


Example:

#include<stdio.h>
#include<stdlib.h>
int main()
{
  //first example
  char *a[3] = {"one", "two", "three"};
  printf("a[0] = %s\na[1] = %s\na[2] = %s\n", a[0], a[1], a[2]);

  //second example
  char **b = malloc(sizeof(char *) *3);
  b[0] = "four";
  b[1] = "five";
  b[2] = "six";

  printf("b[0] = %s\nb[1] = %s\nb[2] = %s\n", *b, *(b+1), b[2]);
  return 0;
}


Output:
a[0] = one
a[1] = two
a[2] = three
b[0] = four
b[1] = five
b[2] = six


C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tony Nazzal

Expertise

I will answer any c language programming and syntax questions.

Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.

©2016 About.com. All rights reserved.