Runtime error when creating dynamic multidimensional array of pointers (char**) [closed] - 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 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.

Related

I am at a loss for why this is code is giving me a read acess violation. dereferencing pointer and subtracting another char should work in Theory [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 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));

Why printing a string show junk [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 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.

malloc() on double pointer [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 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);

Getting error when trying to malloc memory to pointer variable declared separately [closed]

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.

Casting int to char* [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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.
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
Improve this question
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I can cast int to char but I need cast int to char*.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
char ch_number_of_rev[4];
version = "0.";
itoa(number_of_revision,ch_number_of_rev,10);
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
but I need cast int to char*
Casting only changes the type - it does not change the value within the variable. If you need to convert an int to array of chars (i.e. a string) then use sprintf or snprintf:
char* buffer = ... allocate a buffer ...
int value = 970;
sprintf(buffer, "%d", value);
Converting int to string in c
Also, you have not allocated any memory for version - use malloc and allocate some memory.
strcat here won't work because you haven't allocated any space to store the result in. Your version is probably in read-only memory anyway, so you'd get a segfault, otherwise you'll get memory corruption. So make sure to allocate enough space for it, e.g. by using
char version[10] = "0.";
You may want to read up on pointers first, though.

Resources