array subscript is not an integar [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 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.

Related

code gives segmentation fault(code dumped) [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 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.

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.

Why is the sizeof() this struct 8? [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 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!

simple code doesn't work. Dunno why [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 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

Why is the exception stating "Run-Time Check Failure #2 - Stack around the variable 'f' was corrupted." thrown here? [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 8 years ago.
Improve this question
I have written this code:
void f1(void)
{
sprintf("\nf1.");
}
void f2(void)
{
sprintf("\nf2.");
}
void f3(void)
{
sprintf("\nf3.");
}
void f4(void)
{
sprintf("\nf4.");
}
int main()
{
// int i;
void (*f[4])(void);
f[1]=f1;
f[2]=f2;
f[3]=f3;
f[4]=f4;
(*f[1])();
(*f[2])();
(*f[3])();
(*f[4])();
/*for(i=0;i<4;i++)
{
(*f[i])();
}*/
getch();
}
The program compiles, runs, and shows the output
f1.
f2.
f3.
f4.
but when I press enter, it throws the exception stating "Run-Time Check Failure #2 - Stack around the variable 'f' was corrupted."
Please explain to me the reason for this, and also when to use snprintf and sprintf.
Also, let me know why I cannot use
for(i=0;i<4;i++)
{
(*f[i])();
}
for function pointer. It's throwing an error for this as well.
void (*f[4])(void);
f[4]=f4;
Arrays start from 0 in C. Accessing f[4] is illegal.
In addition to your out of bounds array problem, you must have this line:
#include <stdio.h>
because you're using the sprintf function. If you do this, your compiler should give you an error to say you've called it incorrectly. Your compiler has probably warned you that it's used an implicit declaration. As it stands, it's going ahead and calling the function incorrectly and hoping for the best. This may also be causing a stack problem.

Resources