How can i declare a array of string. and how can i access in this array? assign value in array of string ?

what is the problem with this code?

#include <stdio.h>
int main(){

    char name[20][4]={'zero','one','two','three'};

    printf("%s",name[0]);
    printf("%s",name[1]);
    printf("%s",name[2]);
    printf("%s",name[3]);

    return 0;
}

asked 01 Apr '16, 12:43

tanvirul147453's gravatar image

tanvirul147453
456

edited 02 Apr '16, 07:23

__salman__'s gravatar image

__salman__ ♦♦
1.1k211


You make a little mistake on declaration name[20][4] array. The last value of this array means the maximum number of character that one string can have.

Here you use four, but the string "three" contains 5 characters.

change if from

char name[20][4]={'zero','one','two','three'};

to

char name[20][10]={'zero','one','two','three'};

and it will work fine.

for more you can follow some bangla tutorials

  1. String Basic Difference Between char array & string
  2. String Basic Input Output
permanent link

answered 01 Apr '16, 13:37

Sharif%20Chowdhury's gravatar image

Sharif Chowdhury
3.5k111

thanks for your nice answer.

(01 Apr '16, 13:57) tanvirul147453
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×402
×56
×56
×6
×2

question asked: 01 Apr '16, 12:43

question was seen: 979 times

last updated: 02 Apr '16, 07:23