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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

An easy string-array question

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
NetoMan
Asked:
NetoMan
  • 2
1 Solution
 
inprasCommented:
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
 
NetoManAuthor Commented:
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
 
inprasCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now