Initializing a 2-D character array

While writing a code, i was incorporating 2 dimensional array but it just wouldn't work.

The code was:

char acUsername[3][2]={"john",john1","smith","smith2","dennis","dennis3"};

Any idea where I went wrong?

Replies

  • MaRo
    MaRo
    You may've better to post the actual error, but anyway...

    - You've to put '*' next to char 'char*', because you're iniatlizing an array of arrays, every string in your array is actually an array of chars.

    - try to complete the brackets - char* acUsername[3][2]={{"john","john1"}, {"smith","smith2"},{"dennis","dennis3 "}};

    - Remember - acUsername[row][column]
  • silverscorpion
    silverscorpion
    char acUsername[3][2]={"john",john1","smith","smith2","dennis","dennis3 "};
    I think this is a 3D array and not a 2D array. If a char array is of dimension [3][2], then it canhold a total of 6 characters only. For your array, you have to initialize it as [3][2][6]. That should solve the problem. Or if you want to work with pointers, what MaRo said is also absolutely correct. You can initialize it as a char pointer pointing to a char array.
  • aashima
    aashima
    Yeah, I figured out that it wasn't actually a 2D array that I was trying to implement. I have taken two separate 2D arrays and they are working fine.
    Thanks to both of you. 😀
  • ProgramCrazy
    ProgramCrazy
    I know your problem is solved but this could be another way.
    Your array is a two dimensional array but you have not given the size properly.
    Look at the following statement.This works....

    char acUsername[6][7]={"john",john1","smith","smith2","dennis","dennis3 "};

You are reading an archived discussion.

Related Posts

Hello I am a computer engineer from NIT kururkshetra.I have developed a platform which is yielding 30%-120% return per month with drawdown < 10%,thus highly safe.I have backtested it on...
Hi guys i am chetan from pune.a chemical engineer by profession & presently working in pune.i love reading, playing guitar,gr8 fan of rock bands,& above all i love to make...
Hi All, I have a question here , how people gets banned , i have seen a person named Praveena banned i saw her advertising something about VLSI is that...
What is the difference between PNP & NPN inductive proximity switch? How it differs in application wise? which is the best for automation industry?
Any body can tell what is burden in current transformer?