Solved

An easy string-array question

Posted on 2000-04-11
3
174 Views
Last Modified: 2010-04-02
I´m getting on the run with C and I need a tip for this. (Well, I think pointers and arrays in C are dificult to understand in the first days of training)

I have this declaration :

char strings[50][2];

Then.. how can I store values in this array that makes it look like this ?

 strings[1] = 'nel'
 strings[2] = 'sol'
 strings[3] = 'xel'

I can´t change the declaration to a similar one because I have some restrictions in PRO*C/C++ for Oracle, So the next declaration wont work:

 char *strings[3]   ---> This not good

And I need access the value in this way

  printf("\n",strings[1]);

I need also use (if I need to) compatible UNIX functions.

Regards!

0
Comment
Question by:NetoMan
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
inpras earned 25 total points
ID: 2705826
Hi
use it like this
char strings[50][50];
strcpy(strings[1], "nel");
strcpy(strings[1], "sol");
strcpy(strings[1], "xel");
Hope this helps
0
 
LVL 2

Author Comment

by:NetoMan
ID: 2705906
Well... I´already tried that and didn´t work but in the run I notice that I have a error on my code :

char strings[50][2]

strings[1] = 'nel'
strings[2] = 'sol'
strings[3] = 'xel'

and when Im doing :

  strcpy(strings[1], "xel");

it didn´t work because of the difference in th length in 'xel' and the declaration of strings.

The point in here is why C does´nt check this insetad of writing in the strings :

strings[1] = 'nelsolxel'
strings[2] = 'nelsol'
strings[3] = 'xel'

well... that´s another question.

inpras : I´ll give you points in a grade C because I resolve my problem on my own but apreciate your help.
0
 
LVL 4

Expert Comment

by:inpras
ID: 2705972
coming to answer of UR another question C being very flexible in pointers it won't check for array upper bounds ofcourse richie could have implemented it very easily but as U go on mastering it U will feel the advantages of it.
like accessing memory direcly and so on
Hope I cleared a little what doubt of URs
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now