This question already has answers here:
Assigning strings to arrays of characters
(10 answers)
Closed 9 years ago.
int main()
{
char s[]="stack";
s="overflow";
}
This is not allowed.Its gives an error.But below code works fine.
int main()
{
char s[]="stack";
strcpy(s,"overflow");
}
Why this happens?
The variable s represents a pointer to the string. More specifically, it refers to the memory address of the first letter in your string "stack". Due to this reason, the operation s="overflow" makes no sense. How can you set the value of s (a pointer) to a string?
Keep in mind that C is a very low-level language, so you have to be wary of things that might seem intuitive to you not behaving the way you expect them to.
Related
This question already has answers here:
Array index out of bound behavior
(10 answers)
Closed 2 years ago.
the code written below copies string from one array to another and i have declared the both arrays as static and i have given the size of the second array as '1' while the string size which is to be copied is greater than the size i have provided to the second array,still the program is running without any error and also coping the whole sting string and displaying it. if both the arrays are static then the size of second array 't' is increasing so that the it can holds the whole string.
#include<stdio.h>
void main()
{
char c[]="hello";
char t[1];
int i;
for(i=0;c[i]!='\0';i++)
{
t[i]=c[i];
}
t[i]='\0';
printf("%s",t);
}
then the size of second array 't' is increasing
No. The size is not increasing. You wite to the outside of the bounds of the array, and then read out of bounds and the behaviour of the program is undefined.
P.S. The program is ill-formed because main does not return int as is required.
if it is a an undefined behaviour then how the code is working and producing valid output
Because it is undefined behaviour. Any behaviour is possible when it is undefined. You cannot assume that the output wouldn't be valid because that is not guaranteed. Nothing is guaranteed when behaviour is undefined.
This question already has answers here:
Why don't I get a segmentation fault when I write beyond the end of an array?
(4 answers)
Closed 6 years ago.
I have declare an int array of just three elements but I notice that I can access to array indexes bigger
int x[3];
int length = sizeof(x)/sizeof(x[0]);
printf("\n the length defined is %i but I can still setting and getting other indexes")
One of the things that makes C fast is that it doesn't do any type of bounds checking on arrays. It expects programmers to know what they're doing to stay within the proper bounds.
Failure to do so means accessing a portion of memory outside the bounds of the array and leads to undefined behavior.
When accessing an index bigger than an array length, what you are actually doing is to "invade" a memory area that you are not supposed to access. This can lead to unexpected behavior of your application or a crash.
This question already has answers here:
I want this to crash, but it doesn't
(4 answers)
Closed 7 years ago.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[10];
a[2] = (int*)malloc(sizeof(int));
a[2][3]=4;
printf("%d", a[2][3]);
return 0;
}
I have given only the memory equivalent of a single int to the pointer variable. How is that I am able to access an element at index 3 for that pointer of a single int?
That's because nothing is preventing you accessing arrays out of bound and this invokes undefined behavior. Any expected or unexpected behavior of program can be seen.
You can try but the behaviour of your program will be undefined.
C does not perform that kind of checking at runtime.
You're accessing some randomly set RAM element. This is a common C programming mistake and is one of the reason for many security flaws.
The Heartbleed bug would be a nice example, where people were able to read a good portion of the server RAM, by accessing elements outside of the array structure.
This question already has answers here:
Assigning strings to arrays of characters
(10 answers)
Closed 7 years ago.
Well first i'm using language c i still a beginner.
char S[20];
S ="ewer" ;
is that correct.
Arrays (including strings) can't be assigned in C. For strings you need strcpy, or preferably strncpy:
#include <string.h>
...
char S[20];
strcpy(S, "ewer"); // strcpy is fine for this example
strncpy(S, "ewer", sizeof(S)); // strncpy is safer in general and
// should be preferred over strcpy
No, that won't work.
The variable S is an array, and you can't assign to arrays like that in C. The string "ewer" is represented as an array of characters terminated by the character '\0'. To copy it into the array, you need to use a function:
strcpy(S, "ewer");
That's a good question. In fact, if you'd wrote char* S instead, the example would worked. You might be confused by the fact, that arrays and pointers have many alike things — like [] operator.
But you must understand that array is different from a pointer. One of the main differences is that you couldn't increment an array, like ++myArr (this code okay for pointer, but not for an array). The other one you see in the question: you can not reassign an array variable to point another array. That is exactly what you're trying to do: you're assigning to the array variable S a pointer to a place with the text "ewer", that won't work.
Assuming that you wanted to assign to an array the text, you could do the following:
char S[] = "ewer";
Here you say to the compiler to allocate as much space on the stack, as the "ewer" text (plus the zero end character) holds, and copy that text there. Notice the empty braces [] — you don't even need to manually count symbols, it would be done for you by a compiler.
This question already has answers here:
Difference between int* a and int *a [duplicate]
(2 answers)
Why is the asterisk before the variable name, rather than after the type?
(12 answers)
Closed 9 years ago.
I most commonly see code with the pointer next to the variable name instead of the type.
I originally preferred the later (·char* ch·) because to me it makes more sense that I am declaring the type as a character pointer and just naming the variable.
That said, I feel like I must be missing something if so many people see it the other way. What are other ways of looking at it that might provide more meaning?
If you write
char* ch;
or
char *ch;
does not cause any issues.
However, if you need to declare multiple pointers,
char *ch1,*ch2;
there is a chance you make a mistake like this,
char* ch1,ch2;
In this example, ch1 is a pointer but ch2 is plain int.
IMHO, it's better to use the * near the variable name to make sure you don't make a similar mistake