Adding char to an array variable

Pages: 12
When I get a user's input of their char, how can I add it to an array variable?

For instance:

1
2
3
4
5
6
7
8

#define S 20
#include <stdio.h>

char var;
char var2[S];
scanf( "%c", &var);


would it be as simple as doing var2[S]=var?
In this case, var2[S] = var; would write over memory that isn't yours and if you're lucky, cause your program to crash with a segFault. It is, however, as simple as var2[S-1] = var;
What do you mean it would write over memory that isn't mine? Is it because I forgot to put the S-1?
When you make an array like this:

char var2[S];

You get a number of elements (or spaces) in the array. You get S elements. They are labelled as follows:

var[0]
var[1]
var[2]
...
...
var[S-1]

So, for example, if you had
char var2[4];
the following is writing to all memory in that array:
1
2
3
4
var[0] = var;
var[1] = var;
var[2] = var;
var[3] = var;


There are 4 elements, from 0 to 4-1.

If you try to write to var[4], you are writing to the next element along, which is after your array, which is memory that is not part of your array and may be memory the operating systems thinks is not yours. In that case, there will be a segFault.

If you are unlucky, you will be writing over memory that the operating system thinks is yours, and you will trash whatever data of yours is actually in that memory. This is a called a "buffer overflow" error and is a very common error in C and C++ programming.

Ah ok, because an array always start at 0, I have to keep that in mind.

So I went ahead and tested something myself.
1
2
3
4
5
6
7
char var;
char var2[S];
scanf( "%c", &var);
var2[S-1]=var;
printf("%c", var2[S-1]);
scanf( "%c", &var);
printf("%c", var2[S-1]);


why would it not allow me to store multiple chars in the same varible if it can S is defined.. say 20?
Nvm, I tried a different here:
1
2
3
4
5
6
7
	int i;
char var[S];
for(i=0; i<5;i++)
{
	scanf(" %s", &var[i]);
}
printf("%s", var[i]);


but now I have no idea on how to list the arrays.
why would it not allow me to store multiple chars in the same varible if it can S is defined.. say 20?


Perhaps you misunderstand what an array is. When you create an array like this

char var2[S];

you get an amount of space that will fit S char.

To put something in the first space, use var2[0]

If you then put something else in the exact same space, of course you write over what was originally there. If you understand why this doesn't work:

1
2
3
int x;
x=4;
x = 5; // Hey, where did the 4 go? Why can't I put two different int values into one variable? 


then you understand why this also doesn't work

1
2
3
4
5
scanf( "%c", &var);
var2[S-1]=var;
printf("%c", var2[S-1]);
scanf( "%c", &var);
printf("%c", var2[S-1]); // Hey, where did the first value go? Why can't I put two different values into one variable? 


If you want to store another char in the array, put it into a different space. For example,

1
2
var[0] = 'a'; // Store one value in the first space...
var[1] = 'b'; // and store another value in a different space 



but now I have no idea on how to list the arrays.


What arrays? There is only one array. You made it like this:
char var[S]; var is an array (just one array). It is the exact same thing as S char variables all next to each other in memory.

What do you mean "list the arrays"? Do you mean you want to output the values? You can do that like this:
1
2
3
4
5
printf("%s", var[0]);
printf("%s", var[1]);
printf("%s", var[2]);
printf("%s", var[3]);
printf("%s", var[4]);


I see that you already understand how to make a for loop. Perhaps you'd like to use a for loop.

1
2
3
4
for(i=0; i<5;i++)
{
printf("%s", var[i]);	
}






Last edited on
Found out!

1
2
3
4
5
6
7
8
9
10
	int i;
	char var[S];
	for(i=0; i<5;i++)
	{
		
		scanf(" %c", &var[i]);
				var[5-1]='\0';
	}
printf("%s", var);


now my next advance question is, lets say I store a character in a variable (that is not an array) THEN I want to store that variable in an array, how can I approach to doing this?
Do you mean store the variable in the array, or store the value that the variable holds in the array?

int x = 5; // Do you want to store the actual int called x in an array, somehow moving it, or do you want the number 5 to be stored in the array?
Last edited on
store the value that the variable holds in the array

for instance:

1
2
3
4
5
6
7
8
9
10
{
	int i;
	char var;
	char var[S];
	for(i=0; i<5;i++)
	{
		
		scanf(" %c", &var);
	}
}


the character is stored in var, but I want to somehow transfer those char to var[S]. I tried doing var[S]=var; and of course that doesn't work.
Last edited on
var[S]=var;

Didn't we cover this at the start? We did. I wrote something like this:

When you make an array like this:

char var2[S];

You get a number of elements (or spaces) in the array. You get S elements. They are labelled as follows:

var2[0]
var2[1]
var2[2]
...
...
var2[S-1]

So, for example, if you had
char var2[4];
the following is writing to all memory in that array:

1
2
3
4
var2[0] = var;
var2[1] = var;
var2[2] = var;
var2[3] = var;



There are 4 elements, from 0 to 4-1.

If you try to write to var[4], you are writing to the next element along, which is after your array, which is memory that is not part of your array and may be memory the operating systems thinks is not yours. In that case, there will be a segFault.

If you are unlucky, you will be writing over memory that the operating system thinks is yours, and you will trash whatever data of yours is actually in that memory. This is a called a "buffer overflow" error and is a very common error in C and C++ programming.



I must have not been clear enough. I'll try again.

When you make an array of length S, the last element in it is S-1.

For example, if you make an array of length 3, the last element is the only labelled 2. The first element is labelled 0;

So, when you make an array of length S, the last element is S-1.

I tried doing var[S]=var; but that doesn't work out..

Because the last element is var[S-1]; Try
var[S-1]=var;



Last edited on
Then S comes up as an error saying that it needs a pointer-to-object

this is what my whole code looks like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
#define S 20
 
int main() 
{
	int i;
	char var;
	char var[S];
	for(i=0; i<5;i++)
	{
		scanf(" %c", &var);

	}
	var[S-1]=var;
}

Last edited on

This code stores the value that is in x into the first element of the array.

1
2
3
4
const int S=20;
int x = 5;
int anArray[S];
anArray[0]=x;
Oh I am so sorry, I meant to say this; I want to store the char var, into a different variable name that is an array! I mistype what I meant to say
Sorry about the mixup, here is what I mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
	int i;
	char var;
	char differentvar[S];
	for(i=0; i<S;i++)
	{
		scanf(" %c", &var);

	}
	differentvar[S-1]=var;
	differentvar[S-1]='\0';
	printf("%s", differentvar[S-1]);
}


My attempt at trying to store var into the array differentvar is unsuccessful
Last edited on
1
2
3
4
const int S=20;
char var = 5;
char anArray[S];
anArray[0]=var;


This code stores the value of var in an array, and that array has a different name.

You cannot move the variable var into an array (except by some horrible low-level, very risky pointer tricks that are dangerous and foolish and will usually trash your data if they don't cause a crash). All you can do is copy the value.
Last edited on
1
2
differentvar[S-1]=var; // store var...
differentvar[S-1]='\0'; // ... and immediately WRITE OVER IT WITH THE VALUE '\0' 


You are storing var and then writing over it.
Last edited on
So in my case this, I guess pointer is absolutely necessary? Since you said we can't simply transfer a single char to a variable that holds array
So in my case this, I guess pointer is absolutely necessary?


No. Just don't write over the value.

Try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
{
	int i;
	char var;
	char differentvar[S];
	for(i=0; i<S;i++)
	{
		scanf(" %c", &var);

	}
	differentvar[0]=var;
	differentvar[S-1]='\0';
	printf("%s", differentvar[S-1]);
}


See what I did here? I put the value of differentvar in var[0], and then I wrote '\0' into somewhere else. Because I wrote it somewhere else, I did not write over var[0].

I think arrays are beyond you. You need to go back and start again with what a variable is and what memory is.
Last edited on
I will admit, arrays are very hard for me to grasp. I definitely thank you for your time in teaching me something about arrays. I will be glad to say that I learned a whole lot today.
Pages: 12