Solved

An easy string-array question

Posted on 2000-04-11
3
178 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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