Solved

filling an array the right way

Posted on 2002-06-08
17
226 Views
Last Modified: 2011-09-20
i want to have my array:

type to be filled with: PB VW VO VA VA PW
example:
type[1]=VW
then, but i gt just one char :S
i use

char type[6] = {PB,VW,VO,VA,VA,PW}

any sugestion ??
0
Comment
Question by:markyvt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +3
17 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 50 total points
ID: 7064962
Have you tried...

char * pstrMyType[] = {"PB", "VW", "VO", "VA", "VA", "PW"};
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7064969
Example:

#include <stdio.h>

int main(void)
{
     static     char * pstrMyType[] = {"PB", "VW", "VO", "VA", "VA", "PW", ""};
     auto          int     intLoop=0;

     while(pstrMyType[intLoop][0])
          {
          printf("%s\n", pstrMyType[intLoop]);
          intLoop++;
          }
     return 0;
}
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7065069
Or:

enum MyTypes
{
    PB,
    VW,
    VO,
    VA,
    PW
};

char type[6] = { PB, VW, VO, VA, VA, PW };

This assumes you don't actually need to use the string representations of these values anywhere, and that the two VAs in your original text are both supposed to represent the same value.
0
Industry Leaders: 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!

 
LVL 2

Author Comment

by:markyvt
ID: 7065494
i just wanted to
random i;
struct1.type=type[i];

and then write it to a file
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7065717
If the stryct1.type is a number and type[] is an array of numbers, you CAN do that.

If it is a string and you're using plain-old C, you'll need to either use a string copy command or move all of the bytes of the string over.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7065722
The big question that needs to be answered is:
 What is (PB,VW,VO,VA,VA,PW)?
Are they (variables representing) numbers or strings?
Can they be enumerated types or can this be a bit map?
0
 

Expert Comment

by:amitv
ID: 7070462
Your Question doesnt tell anything about array "Type". Nor does it give any information regarding the possible values of the "Type".
0
 
LVL 2

Author Comment

by:markyvt
ID: 7070466
i fixed it by using
strcpy(array[1],"PB")
0
 

Expert Comment

by:amitv
ID: 7070487
Your Question doesnt tell anything about array "Type". Nor does it give any information regarding the possible values of the "Type".
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7070513
Ok.
So what we were looking for is that your data is a string array.  
You can also fill the array wth something like:
   array[0][0]='P';
   array[0][1]='B';

If you need it to be NULL terminated add:
   array[0][2]='\0';
/* If it's at least 3 characters big */
0
 
LVL 2

Author Comment

by:markyvt
ID: 7070699
the PB etc. resemble an string of 2 chars

now you guys tell me who answered correct ??
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7071178
Well...
"char *" represents "string" as was stated in my first comment.
0
 

Expert Comment

by:amitv
ID: 7071871
In Comment of 6/8/02 Triskelion has give a big clue to how to deal with the strings and array of pointer's to charaters. Here is wot I think

/* Possible types */
static char* possibleTypes={"PB", "VO", "VA", "VW", "PW"};

Assuming you wud be having some structure of this sort...

struct dataWithTypeInfo
{
     Data data;
     char* ofType;
};

Now this kinds of assignment operator will work wihout any hassle....(see below)

dataWithTypeInfo.ofType = possibleTypes[ i ];
// where i is any number b/w 0 and 4

think this will give you other option then using strcpy()...
thanx...




0
 

Expert Comment

by:C1AzadNizam1C
ID: 7142533
I do not completely understand your question but gave you an answer from what I understand.
Fill arrays with {PB VW VO VA VA PW} you could try
/**********/
#include"stdio.h"
#inculde"stdio.h"
#include"stdio.h"
Main()                              /************/
                                     /********/
{                      
chartype(6)=(PB VW VO VA VA PO,\n)
Printf(*********)
Scanf(your input)
Printf(**********)
Scanf(your input)
*
*
}
END

Some where in the printf input from computer it could say something you may not have read.
Asad.
 


 
0
 

Expert Comment

by:C1AzadNizam1C
ID: 7156175
You could use:
 
strcpy(array[1],"PB");

Asad.
0
 

Expert Comment

by:C1AzadNizam1C
ID: 7161213
I  think using strcpy() would solve the problem as I said before.
Asad.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9480238
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept Triskelion's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

691 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