x variable defined as array[size]

I would like to know how to insert a variable inside the size of a array!
Since im writting a guessing game, P1 needs to insert how many letters is his word
kind like that, its not finish, im just stuck as!!
cheers guys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void get_word (void)
{	
	int x;
	char word[x];
	int i;
	
	printf("How many words?\n");
	scanf("%d%*c", &x);
		
	for(i = 0; i < word[x]; i++)
	{
		scanf("%c%*c", &word[i]);
	}
		
	printf("the word are: %c\n", &word[x]);
		
	
	return;
}
Last edited on
To create a dynamic array you must do something like this (I will use cout instead of printf and cin instead of scanf but you can convert fairly easy.)

1
2
3
4
5
6
7
8
9
unsigned lettersInWord = 0u;
std::cout << "How many letters in your word? ";
std::cin >> lettersInWord;

char *word = new char[lettersInWord];


//When ever you are done with the word variable
delete[] word;
I would however suggest you use a dynamic container such as a std::vector if you are going to be using a dynamic array. http://www.cplusplus.com/reference/vector/vector/?kw=vector
http://homepages.e3.net.nz/~djm/cppcontainers.html

For this assignment if you can please use a std::string Though from looking at the code I am assuming you are learning c instead of c++. I could be wrong though.
1
2
3
std::string word = "";
std::cout << "Please enter your word: ";
std::cin >> word; //you can use std::getline to be safer and make sure there isn't any erroneous inputs. 
http://www.cplusplus.com/reference/string/

Line 18 in your function is pointless. It has a return type of void(nothing) so you shouldn't return at all, none the less an empty return.


*edit forgot closing code tag

Last edited on
giblit's solution is very good if you are using C++, however it looks like you are using C here.

C does not have dynamic memory and you need to know the size of the array when you compile (it can't be done while running your program).

Typically, you'd do something like this:
1
2
3
4
5
6
7
8
9
10
char word[100];
while (true)
{
  printf("How many words?\n");
  scanf("%d%*c", &x);

  if (x < 100) break;

  printf("I can't handle that many. Try again.\n");
}
@Stewbond

I was always under the impression that is what malloc is for:

http://www.cplusplus.com/reference/cstdlib/malloc/


Also, scanf returns a value for how many items were read, so one should always use this to see if it worked - especially for multiple values and FP numbers.
Hi Stewbond, u are right. Im using C. That looks pretty cool..im gonna have a go and see if works, thanks sooo much

Hi Ideasman, I have not idea what mallocs are, but im going to have a look.

There are soo much to learn, programming is very complex and there are soo may ways thing can be done. So far I have only worked w C at uni and C sometimes is unfair w the little details.
Does that happens w C++ or other languages??
Hi radc,

As the example shows malloc and it's partner free is for dynamic memory allocation / deallocation. One has to use pointers to deal with them, but if you are using C, then you should be all over that.

EDIT: one can use the sizeof operator to help decide how much memory to allocate. If you wanted an array of structs, then multiply the size of the struct by how many you want. Because you cast the pointer to the type you want, you can increment the pointer to refer to the next struct. So you can use a for loop to initialise or retrieve your data.

When one increments the pointer returned by malloc, it points to the next item in the array - even if it is not a basic type. So one could have an array of structs of a reasonable size, incrementing the pointer still gives the next struct.

IMO C++ is much more friendly than C, especially with STL containers - they have all kinds of built in functions and one can apply algorithms to them.

Btw I am not claiming to be any kind of expert - far from it I am only a Dabbler. Stewbond has vastly more knowledge than me, I always look forward to what he has to say, and he has helped a lot of people over time. I think it is important for one to have a realistic view of their relative "position".
Last edited on
Is there a better way to print a array?? In C, why cant we pass the array by reference?? and why is my array not going back to main??
Arrays are hard to work with!!! urrrghhh
Thanks for looking!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <ctype.h>

void welcome_screen(void)
{
	printf("Welcome to the Hangman Game\n");
	printf("Player 1 will entry his word\n");
	printf("Player 2 \n");
	
	return;
}

char get_word (void)
{	
	char word[20],right;
	int x,i;	
	
	do
	{
		printf("How many letters in your word?\n");
		scanf("%d%*c", &x);
	
		for(i = 0; i < x; i++)
		{	
			printf("Entry letter by letter?: ");
			scanf("%c%*c", &word[i]);
		}
			
		printf("The word is: %c%c%c%c\n", word[0],word[1],word[2],word[3]);
		
		printf("Enter y if correct otherwise enter n: ");
		scanf("%c%*c", &right);
		right = tolower(right);
		
	}while(right != 'y');
	printf("The word after while is: %c%c%c%c\n", word[0],word[1],word[2],word[3]);
	return (word[20]);
}

int main()
{
	char word[20];
	
	welcome_screen();
	word[20] = get_word();
	printf("The word in main is: %c\n", word[0],word[1],word[2],word[3]);
	
	return(0);
}
Hi,

Is there a better way to print a array??


Use a for loop, or use %s in the printf statement - I haven't tried it can't remember (it has been bleems since I have done any C) but I think one can send a char array to it.

In C, why cant we pass the array by reference??


The array name is actually a pointer to the array, so it is pass by reference.

and why is my array not going back to main??


The return value of you r function is char. And you should send the array as a function argument.

Also, scanf returns a value of how many values were successfully read, you should make use of this to see if it worked.

Always initialise your variables to something - C will bite you hard if you don't.

Hope all goes well :+)
line 37: You're returning char[20]. That is the 20th character (not an array of 20), which by the way is out of bounds for the array. The elements of the array are 0-19.

I suspect what you were trying to do is to return the array. However, there's a problem. word is declared as a local array at line 15, so it goes out of scope as soon as you exit get_word. The solution to this is to pass the array as an argument.
 
void get_word (char * word)  // pass in by pointer to array  


Line 45 becomes:
 
  get_word(word);  //  pass base of array (pointer) 


Last edited on
Topic archived. No new replies allowed.