Solved

filling an array the right way

Posted on 2002-06-08
17
224 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 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