Solved

filling an array the right way

Posted on 2002-06-08
17
221 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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:Kdo
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now