0

I am sure it's pretty obvious, but I haven't been able to figure this out for a couple hours. I need to take input (words, strings) from the user and store it in the array, and then I need to access these elements by their index or something similar.
I've tried both frets and scanf but couldn't make it work.
Here's what I have so far:

typedef struct 
{
   char components[MAX_STRING];
   int numComp;
   char weightingSheme[MAX_STRING][50];
   int numOfSchemes;
}  CourseComponents; 

char input [MAX_STRING]
CourseComponents newCourses;
int *Comp = &newCourses.numComp;
*Comp = 0;
int *numOfSchemes = &newCourses.numOfSchemes;
*numOfSchemes = 0;
for (i=0;i<newCourses.numComp;i++) 
{
    printf("Enter next component name: ");
    scanf("%s", input);
    strcpy(&newCourses.components[i], input);  
}
4
  • 1
    What is 'newCourses'? Commented Oct 23, 2014 at 3:26
  • What is the type of newCourses.components[i], is it an array char or an array of pointers? Commented Oct 23, 2014 at 3:28
  • it's structure. i'll edit post Commented Oct 23, 2014 at 3:29
  • char components[MAX_STRING][50]; will not solve your problem? Commented Oct 23, 2014 at 3:37

1 Answer 1

2
char components[MAX_STRING];

This only holds a "single" string. You probably want to declare it like this;

char components[MAX_ELEMS][MAX_STRING];

That's an array of strings, more precisely an array of array of chars (each string a fixed length which can be wasteful). This will get you up and running but it's not what I would call "production quality" You likely don't want hardcoded lengths like this.

Sign up to request clarification or add additional context in comments.

2 Comments

yea, i don't really understand callock by now :) just go with it
how i then copy elements to 2-dimensional array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.