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 5 years ago.
Improve this question
Hello I am new to C and I am trying to print a string that i set by my self but it prints junk.
I know id[4] is '\0' so i did not set it.
int main(){
char id[5];
printf("Enter a string\n");
id[0]=1;id[1]=2;id[2]=3;id[3]=4;
printf("You entered the string %s\n",id);
}
I know id[4] is '\0'
Well, you're wrong.
id being an automatic local variable, unless initialized explicitly, it contains indeterminate value. So, you cannot be sure of any value, let alone '\0'.
Quoting C11, ยง6.7.9
If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. [....]
However, if you initialize it like
char id[5] = {0};
then, by rule of initialization, all the elements are 0-initialized and you can then rely on the null-termination.
Related
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 3 years ago.
Improve this question
I am trying to create dynamic multidimensional array char** variable to store three strings, but an unknown error occurs at runtime.
// Allocate memory for three strings
char **str = (char**) malloc(sizeof(char*)*3);
for(int i=0;i<3;i++)
str[i] = (char*) malloc(20);
// Assign value to each string item
strcpy(str[0], "LionKing");
strcpy(str[1], "Godzilla");
strcpy(str[2], "Batman");
// Print the strings
for(int i=0;i<3;i++)
printf("%s\n", *str[i]);
// Free the memory of the three strings
for(int i=0;i<3;i++)
free(str[i]);
// Free the memory of the main pointer
free(str);
What is wrong with my code?
printf("%s\n", *str[i]); should be printf("%s\n", str[i]);.
*str[i] is a char but %s requires a char *. Your compiler should have warned you about this. If it did not, enable warnings in your compiler, and pay attention to them.
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 4 years ago.
Improve this question
I dunno why this doesn't work. the code has a problem with the *c in
charToInt function but should be a legal statement in c. at least so I thought. I am excited to learn something new here.
int charToint(char *c) {
return *c - '0';
}
int main(void) {
char c = '3';
printf("%d\n", charToint(c));
{
You're passing a char to a function that expects a char *. Your compiler should have warned you about this. The value of that char is then interpreted as a pointer value and dereferenced. Dereferencing an invalid pointer invokes undefined behavior, which in this case results in the program crashing.
The function is ultimately trying to work with a char, so change it to accept a char:
int charToint(char c) {
return c - '0';
}
Alternately, you can leave the function as it and pass it a pointer:
printf("%d\n", charToint(&c));
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 6 years ago.
Improve this question
double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));doesn't work. I try to allocate as much memory as p needs. What is wrong with that?
I try to allocate as much memory as p needs.
p itself (as a variable) has got the (own) memory allocated, what you're trying is basically allocate the memory for which p will be pointing to.
Here, p points to a double, so it needs to have a memory area to be able to store a double value. So, the allocation should be equal to the size of a double, i.e,
p = malloc(sizeof*p);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.
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 8 years ago.
Improve this question
code for copying string
#include<stdio.h>
#include<string.h>
int main()
{
char from[100]="we are the people",to[100];
int i,count=0;
puts(from);
//copying string
for(i=0;from[i];i++)
{
to[i]=from[i];
}
to[i]='\0';
//printing the new string
puts[to];
}
why compiler show array subscript is not an integar in this statement ?
puts[to];
but why this does not show error ?
puts[from];
it should be 'puts(to);' I think you mixed up with array and function. '[]' is for array and '()' is for function calling.
Chnage
puts[to]; to puts(to);
puts[to] means you are declaring an array.
[ ] is used for array size declaration.
( ) is used for function calling.