Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am writing this in C:
char *IP = malloc(12 * sizeof(char));
But I get "Initializer is not a constant
Any ideas?
You are trying to assign a value to a variable outside of any function. In this case, you can only assign constant values, which are not the result of function calls or operations. For example, you can do
int i = 3;
but not
int i = pow(2, 2);
For what you want to do, you can declare the variable in the global scope, but then assign a value in the main.
try this
char *IP;
...
IP = malloc(12 * sizeof(char));//in main
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 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 needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
char i[]=pop();
char j[]=pop();
b=atoi(i);
a=atoi(j);
I wanted to pop an char type element from stack and convert it to int type. But it says
invalid initializer.
What is the problem?
If you want a char variable, use a char variable, don't use a char array.
Change
char i[] = pop();
to
char i = pop();
and likewise.
That said, atoi() won't be relevant there. If you want the result to be of type int, simply use an int variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I use C language for a natural language processing project.
I would like to store a dictionary file.
I used the following define statement
#define DICSIZE 46000
The question is about the number 46000 because it is the maximum number I can enter.
If I try a bigger number the program stop running.
How can I solve this problem?
Program stops not because of DICSIZE macro. It's usage.
I guess, some array is allocated locally(i.e In stack) by passing this macro as array size.
int myArray[DICSIZE];
So when the number is increased, you may face problem. I suggest to allocate memory dynamically using malloc().
I suspect you have a large array declared locally in a function like this:
int main()
{
MyRecordType myArray[DICSIZE];
...
return 0;
}
When DICSIZE gets large, myArray gets large, and you run out of stack space.
Use dynamic memory allocation instead:
int main()
{
MyRecordType * myArray = malloc(DICSIZE * sizeof(myArray[0]));
assert(myArray);
...
free(myArray);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a problem in displaying the size of the array correctly.
I know array size is 256000 but it is displaying as 8 when I enter the loop. size will be displayed accurately if dynamic allocation is not used. How do I rectify the bug using dynamic allocation?
This will give you size 10, because the compiler knows it's an array;
char foo[10];
int size = sizeof foo;
This will give you size 4 on a 32-bit architecture, because it's the size of a pointer.
char *foo = malloc(10 * sizeof(char));
int size = sizeof foo;
After this, the usage of foo is identical. You can do foo[2] or *foo or whatever with both versions. But you probably shouldn't take the address of &foo with the 1st variant. And you should free(foo); sometimes with the 2nd.
Always remember: sizeof is not a function, sizeof is always decided in compile time.
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'm not sure what the following does and i'm hoping someone can clarify the purpose of having the asterisk in front of the functions name:
char *Foo(char *ptr) {
return NULL;
}
I understand that you can pass by value the memory location of something in the function argument call and *ptr would be the pointer to it. I understand you can create a pointer function that can be used to point to other functions like a regular pointer points to variable memory location but in this case this is not a function pointer that we can point to other functions, or is it? This seems like a real function.
Foo is a function.
It has input: ptr of type char*
It has output of type char*
char* means "pointer to char"
it returns NULL.
That is the most plain explanation I can think of.
its misleading you, the * by the name isn't related to the name
it means the same as char* Foo(char* ptr)
which means a function which takes a char* and returns a char*