Solved

An easy string-array question

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 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

12 Experts available now in Live!

Get 1:1 Help Now