[Webinar] Streamline your web hosting managementRegister Today

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

filling an array the right way

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
markyvt
Asked:
markyvt
  • 6
  • 3
  • 3
  • +3
1 Solution
 
TriskelionCommented:
Have you tried...

char * pstrMyType[] = {"PB", "VW", "VO", "VA", "VA", "PW"};
0
 
TriskelionCommented:
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
 
pjknibbsCommented:
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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
markyvtAuthor Commented:
i just wanted to
random i;
struct1.type=type[i];

and then write it to a file
0
 
TriskelionCommented:
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
 
TriskelionCommented:
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
 
amitvCommented:
Your Question doesnt tell anything about array "Type". Nor does it give any information regarding the possible values of the "Type".
0
 
markyvtAuthor Commented:
i fixed it by using
strcpy(array[1],"PB")
0
 
amitvCommented:
Your Question doesnt tell anything about array "Type". Nor does it give any information regarding the possible values of the "Type".
0
 
TriskelionCommented:
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
 
markyvtAuthor Commented:
the PB etc. resemble an string of 2 chars

now you guys tell me who answered correct ??
0
 
TriskelionCommented:
Well...
"char *" represents "string" as was stated in my first comment.
0
 
amitvCommented:
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
 
C1AzadNizam1CCommented:
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
 
C1AzadNizam1CCommented:
You could use:
 
strcpy(array[1],"PB");

Asad.
0
 
C1AzadNizam1CCommented:
I  think using strcpy() would solve the problem as I said before.
Asad.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

  • 6
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now