[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

problem with 2D char array

Posted on 2007-10-12
4
Medium Priority
?
1,332 Views
Last Modified: 2008-01-09
Please tell me what's wrong with the initialization of this 2D char array a[10][2];
I've tried with curly brackets too, but nothing happends. With round brackets, ASCII values get stored;

#include<iostream>
#include<conio.h>
#include<string.h>

using namespace std;

class loj
{
     
              char a[10][2];
              string s;
              char b[2];
              int j;
             
      public:
             void input();
             void logic();
             loj();
};
loj::loj()
{
          a[10][2]=(
                    ('n','o'),
                    ('p','a'),
                    ('r','e'),
                    ('c','i'),
                    ('v','o'),
                    ('m','u'),
                    ('x','a'),
                    ('z','e'),
                    ('b','i'),
                    ('s','o')
                   );
                   //to test the value of a[10][2];
          for(int i=0;i<2;i++)
                  for(int j=0;i<10;j++)
                          cout<<a[i][j];
          j=0;
}
void loj::input()
{
     cout<<"enter a lojo string\n";
     cin>>s;
     cout<<"sd";

}
void loj::logic()
{
     cout<<s;
     for(int i=0;i<s.length();i++)
     {
          while(j<=1)
          {
                     
                      b[j]=s[i];
                      j++;
          }
          if(j==2){
                   
                      for(int k=0;k<=9;k++)
                      {
                              if((b[0]==a[0][k])&&(b[1]==a[0][k]))
                              {
                                                                  cout<<i<<"\t";
                              }
                      }
                      j=0;
            }
     }
}
     
int main()
{
    loj l;
    l.input();
    l.logic();
    getche();
}
0
Comment
Question by:shilpi84
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20065366
Actually, you cannot initalize a member that way. However,

loj::loj()
{
          char b[10][2]={
                    {'n','o'},
                    {'p','a'},
                    {'r','e'},
                    {'c','i'},
                    {'v','o'},
                    {'m','u'},
                    {'x','a'},
                    {'z','e'},
                    {'b','i'},
                    {'s','o'}
                   };
          memcpy(a,b,sizeof(a));
                   //to test the value of a[10][2];
          for(int i=0;i<10;i++)
                  for(int j=0;i<2;j++)
                          cout<<a[i][j] << endl;
          j=0;
}

should work. Note that the end indices for i and j in the loop were apparently swapped.
0
 

Author Comment

by:shilpi84
ID: 20065685
thanks jkr... it works now... but why couldn't this work directly without using b and then copying

a[10][2]={
                    {'n','o'},
                    {'p','a'},
                    {'r','e'},
                    {'c','i'},
                    {'v','o'},
                    {'m','u'},
                    {'x','a'},
                    {'z','e'},
                    {'b','i'},
                    {'s','o'}
                   };
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 2000 total points
ID: 20065769
Because using 'a[10][2] = ...' means that you are trying to assing a value to an array *element*, not initializaing an array - the notation 'a[10][2]' even writes over the array bounds, since 'a[9][1]' are th ehighest allowed indices.
0
 

Author Comment

by:shilpi84
ID: 20067307
thanks jkr you  cleared my doubts
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question