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 9 years ago.
Improve this question
Got xCode 5.0.2 bought mac yesterday and do not understand why this simple code doesn't work.
#include "stdio.h"
int main(){
int N;
printf("vvedite koli4estvo dannih\n");//mistake and warning is here
scanf("%d", &N);
int *arr = new (int [N]);
return 0;
}
mistake is
expected expression
implicit declaration of function 'new' is invalid in c99
Your code is written in C but you are using new; a C++ operator. Use malloc instead.
int *arr = malloc(sizeof(int)*N); // allocates memory for N itegers
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 5 months ago.
Improve this question
I dont know why this code gives segementation fault(code dumped) is there something wrong with the syntax
#include <stdio.h>
int checksub(char strng){
int a=strlen(strng);
printf("%d",a);
}
int main(){
checksub("twoi");
}
You're passing a char* argument, so you must accept one. Further, you're promising to return int but fail to do so. Third, you've got a single-use variable that's basically irrelevant, so you can simplify to this:
void checksub(char* strng) {
printf("%d", strlen(strng));
}
Where strlen() returns size_t, you'll actually need:
void checksub(char* strng) {
printf("%zu", strlen(strng));
}
--
There's a lot of things your compiler should have warned you about here, so turn on -Wall or equivalent and pay close attention.
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 1 year ago.
Improve this question
Why this version of C code is not working (cause segmentation fault)
#include <stdio.h>
int main()
{
int *p;
*p = 10;
return 0;
}
while this one is working?
int main()
{
char c = 'c';
int *p;
*p = 10;
return 0;
}
Both code snippets are wrong, we can't say that one is more wrong than the other, p is uninitialized in both cases and therefore it may or may not contain a valid memory address, it's impossible to predict, this means that the behavior is undefined. That being the case, working, whatever that may mean, is well within the realm of possible outcomes.
For int* p, no storage space is allocated for the actual integer number.
You need to modify your code as follows to make it work properly.
In C++, you can use new, but in C you can use malloc.
#include <stdio.h>
int main()
{
int* p = malloc(sizeof(int));
*p = 10;
free(p);
return 0;
}
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 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.
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
struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
where sizeof(*void)=8, sizeof(int)=4, sizeof(char)=1
Why does it print 8?
head is a pointer to the struct t, which is 8 bytes since I'm assuming you're running an x64 program. If you want the size of the underlying type, do this:
sizeof(*head)
Notice that head is a pointer to the struct rather than an actual instance of the struct. This means that sizeof(head) is the size of the pointer, which on your system happens to be 8 (notice that sizeof(void*) is also 8).
Hope this helps!