Using C programming and UNIX/Linux terminal. PLEASE DON\'T FORGET TO EXPLAIN THE
ID: 3901883 • Letter: U
Question
Using C programming and UNIX/Linux terminal.
PLEASE DON'T FORGET TO EXPLAIN THE LAST PART!
/*
String constants
Illustrates: string constants, pointers to strings, and array of chars
*/
#include <stdio.h>
int main()
{
char *a = "I am a constant"; // a pointer initialized to point to
// a string constant; value of a can
// be changed; content of string cannot
// be changed
char b[] = "Hello"; // an array of characters (i.e. a string)
// initialized to "Hello"; value of
// b cannot be changed; content of
// string can be changed
// since dimension is not given, string
// is made just big enough to contain
// all the specified characters
char c[] = {'H', 'e', 'l', 'l', 'o', ''};
// equivalent to definition of b
printf("%s %s %s ",a, b, c);
return 0;
}
Explanation / Answer
#include <stdio.h>
int main()
{
char *a = "I am a constant"; // a pointer initialized to point to
// a string constant; value of a can
// be changed; content of string cannot
// be changed
char b[] = "Hello"; // an array of characters (i.e. a string)
// initialized to "Hello"; value of
// b cannot be changed; content of
// string can be changed
// since dimension is not given, string
// is made just big enough to contain
// all the specified characters
char c[] = {'H', 'e', 'l', 'l', 'o', ''};
// equivalent to definition of b
printf("%s %s %s ",a, b, c);
a[0]='X';
b[0]='X';
return 0;
}
This results in a Segmentation fault and that is because a can't be modified as it is not allocated any memory and you can't access it like this.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.