C Assignment Arrays Error [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
The question is: Declare an array of type 'char' called "letters" with a size of 2. Assign the variable "alpha" as the first element of the array "letters".
I tried both:
char letters[1];
letters = alpha;
and
char letters[] = {alpha};
The program keeps saying that both of these answers are wrong. What am I doing wrong? Or is an error with the check system?

There are several mistakes in your answer:
You missed the "of size 2" part,
The second assignment has to go the other way,
The first element of the array is at index zero.
You need to add initialization to avoid undefined behavior:
char letters[2] = {'x', 'y'};
char alpha = letters[0];

in the first example you are not defining an array of size 2 but of size 1, and (this is the actual error), you're assigning a variable of type char to an array and not to a position of the array...
a simple way:
char letters[2]; // array of size 2
char alpha = letters[0]; // assigning the first position of letters array to the alpha variable

Well, using a 1 instead of a 2 should have tipped you off. And if you want to assign to an element of letters, you probably need to say something more than just the name.

Related

How can I delete the first zero(s) in a string? (Without using atoi) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

How to append a char var[] to a char ? C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char hi[10] = "bye";
char a = 'a';
strcat(hi, a);
Like the example above. How would I do this in C? Is there a more general string I cant let hi be?
a is a char type while strcat expects it's both arguments of type char *. To append the char to an array of characters you can do this
int index = strlen(hi);
if(index < sizeof(hi)-1)
hi[index] = a;
Note that in this particular case the initializer will initialize the first three elements of hi to b, y and e respectively. The rest of the elements will be initialized to 0. Therefore you do not need to take care of the null termination of the array after appending each character to it. But in general you have to take care of that.
int index = strlen(hi);
if(index < sizeof(hi)-1){
hi[index] = a;
hi[index+1] = '\0';
}
strcat(hi, (char[]){a,0});
This would append the a.
Or you can do this
char s[]={a,0};
strcat(hi,s);
Or simply
#define MAXLEN 10
...
size_t len = strlen(hi);
if( len+1 <= MAXLEN-1)
hi[len]=a,hi[len+1]=0;
else
// throw error.
In your case hi[len+1]=0 is not required as it is already filled with \0. Also as mentioned by Serge that you can use simply used the string literal as the second parameter to the strcat function.
strcat(hi,"a");
There is a subtle difference in this two as mentioned by Serge again, that string literals are const but the compound literals are not.

C Programming Book Error or Programming Error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
int main()
{
// Initialize & Declare variable
int m = 5;
// Allocates memory for storage of an integer variable
int *itemp;
// Stores memory address of variable m in memory address itemp
itemp = &m;
// Notice after declaring pointer you don't need to reference it as a pointer
// asterick is also known as indirection operator
// indirect reference: Accessing the contents of a memory cell through a pointer variable that stores it's address
// We can rewrite the contents in the memory cell as such
*itemp = 35;
printf("%d",*itemp);
// Doubles the value of m
*itemp = 2 * *itemp;
printf("%d",*itemp);
return 0;
}
It's returning 3570 instead of 70, which is what the book says it should be returning. What am I doing wrong?
The program is correct. It is printing what it is coded to print.
To clarify,
You have two printf()s, printing 35 and 70.
You don't have a "seperator" [for example, a newline (\n)] in your printf()s to distinguish the outputs of two print statements.
Result: You're seeing the final output 3570 as the combination of the output from two print statements, 35 and 70.
Solution: Add a \n or \t at the end of the format string supplied in printf() to add a visual seperator after each printf() to avoid confusion.
Just do
printf("%d\n",*itemp);
You are seeing 35 and 70 as output in the same line either add a space between them or a newline.

How do you create an array of a certain size using malloc() in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Part of the program I am writing requires an array to be created using malloc instead of the regular way of doing it. I have to have the user enter a number, assign that number the name MAX, and create an array using malloc() with numbers 2 through the number entered. How would I go about coding this?
You create an "array" with malloc() by specifying the size (in bytes) of the array and assigning the return value to a pointer of the appropriate type. If you're intending for this to be an array of objects that are larger than one byte, you can multiply the number of objects by the size of the object, which can be obtained with the sizeof operator.
For example, you can create an "array" of fifty int objects like so:
int *ar = malloc(50 * sizeof (int) );
You can do that by:
T *dynamic_memory;
....... //Get the desired array size from user input and store in 'array_max_size'
dynamic_memory=malloc((sizeof(T) * array_max_size);
T : data type of array
You can then use dynamic_memory for your purpose.

How to create n number of arrays based on user input in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to prompt the user to enter a number, and then have the computer create that many arrays.
For example if the user entered the number 5, I would would want 5 integer arrays called array1[64], array2[64], array3[64], array4[64], and array5[64] to be created.
You can use array of pointers
Like following :-
int n,i;
//enter n
int **array = malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
array[i] = malloc(sizeof(int)*64);
/* Do Stuffs*/
/* Free Memory */
for(i=0;i<n;i++)
free(array[i]);
free(array);
Anytime you say, "I want N variables of the same type named var1, var2, var3, ..., varN", what you really want is an array; in this case, you want an array of arrays.
Assuming that you know the second dimension at compile time (i.e., it's always going to be an Nx64-element array of int), then this is easy:
#include <stdlib.h>
...
size_t numArrs = 0;
// get numArrs from user
int (*arrs)[64] = malloc( sizeof *arrs * numArrs );
Presto - you've allocated an Nx64 array of int that you can access like any normal 2D array:
arrs[i][j] = some_value();
arrs[0] is your first 64-element array of int, arrs[1] is your second 64-element array of int, etc.

Resources