Why is assigning to string as given below not allowed?

Consider the code:



main()

{

char s[8] = “Exforsys”;

}



The above code is not allowed in string handling concept. This is because strings are represented in C programming language as arrays and therefore assigning as above arrays is not allowed.


One can make use of strcpy() which is a string handling function present in string.h header file for this purpose. So the above code can be written as



#include

main()

{

char s[];

strcpy(s, ”Exforsys”);

}



So one must not assign strings which are represented as arrays by using operator ‘=’. Instead C programmers must use strcpy() function for this purpose.


bairaviuthra wrote on Friday, 22 June 2007 01:11:58:
Why not? we can define values to a char array in C.
Consider and invoke the below ex:

#include
main()
{
 char s1[10] = "Hello";
 printf("ns1[1]==%cn",s1[1]);//prints  e
 printf("ns1==%cn",s1);//may print some junk /  no values
}

Questions by GeekAdmin   answers by GeekAdmin

  
Showing Answers 1 - 43 of 43 Answers

Kowshik Prakash

  • Jul 12th, 2006
 

char s[8]="Exforsys";In the above line, only 8 bytes are allocated for the array s. But, the size of the string assigned to it during declaration is 8 bytes + 1 byte (for the NULL character '' that C adds at the end of each string within double quotes). Hence, even though the program compiles without errors, accessing the extra one is illegal since it may refer to any previously occupied memory location.

  Was this answer useful?  Yes

sathish kumar

  • Dec 13th, 2006
 

Hi

I think you cant assign like this

char s[8] = ?Exforsys?;

But you can assign like this

char s[8] = {?Exforsys?};

but you make sure that you allocate enough memory for your array.  for ex. char s[10];

  Was this answer useful?  Yes

selva kumar.s

  • Dec 14th, 2006
 

we can assign the values insted of strings to char,by using the following code

int main()

{

char a[8]='e','x','f','o','r','s','y','s';

}

because the strings are the collection of charecters.so we can do like this to print the value.this may be use for begining programming persons.

  Was this answer useful?  Yes

Ankit Kumar

  • Dec 14th, 2006
 

I think the other reason why we cannot assign Exforsys as string of size 8 because in C the last space for string is reserved for NULL value so it cant be 8 characters long it must me s[9] and the last element in the array must be ''

  Was this answer useful?  Yes

NAGARAJU

  • Dec 15th, 2006
 

main()

{

char s[8] = ?Exforsys?;

}

Dear friend,

        We cannot assing the above string to array ie,to s[8], because anything within quotes i.e., "Exforsys", the compiler treats it as string constant and it returnts the base address by replacing the above string as  "Exforsys "

        therefore s[8] is a constant pointer and "exforsys" is also a contant, we cannot assign a constant to another constant.

        the compiler throws an error, illegas structure operation.

just think about printf.....-----> printf("exforsys"); it means the printf requires the base address from there it prints upto null, the output is exforsys.

  Was this answer useful?  Yes

rambabu

  • Dec 16th, 2006
 

 Hai, this is rambabu

   main()

{

      char s[8]="exforsys";

         This programme shall executed with out any errors and warnings.Because any string variable can hold a string at initialisation (i.e.giving values at variable declaration.)but can't assign(i.e. after declared variable). see below prog'me which is not executed.

 main()

{

     char s[8] ;

      s="exforsys";

}

  Was this answer useful?  Yes

hasmath

  • Jan 5th, 2007
 

String means that set of characters but her thye have given in digit so it will not allow

  Was this answer useful?  Yes

Guest

  • Jan 5th, 2007
 

frnd..i found that this program

main()

{

char ch[8]="exforsys";

}

is working with out any errors....

but..if u write like this..

main()

{

char s[8] ;

ch= ?Exforsys?;

}
then u will found error like...'Lvalue required in function main'...it may be because u r declaring as array of charecters and using as ordinary variable...and  if u still want to assin  u should go like

ch={'e','x','f',...'/0'};or assin individually like ch[0]='e'.....etc

  Was this answer useful?  Yes

Guest

  • Jan 20th, 2007
 

i guess bcoz the array defined is of size  8  and to store a string, itz necessary to include '/O' at the end to indicate end of string

if this is not done, it is not considered as a string declaration but as an array of 8 characters.

if u try to print the value using "%s", it  will print 8 chars followed by absurd charcters until it encounters '/O'..

so, include '/o' to make it a string..

i hope tht answers the q to my lnowledge..

.

  Was this answer useful?  Yes

S.Praveen kumar

  • Mar 9th, 2007
 

It can be  char s[8]={"exforsys"};
    

  Was this answer useful?  Yes

i dnt think this has got anythin to do with the representation as you stated. it is basically because the memory allocated for the character array is less than that required for the declaration of string. it is to be remebered that string declarations MUST end with '/O'.. i guess it will clarify my point.
regards.

  Was this answer useful?  Yes

Why not? we can define values to a char array in C.
Consider and invoke the below ex:

#include
main()
{
?char s1[10] = "Hello";
?printf("ns1[1]==%cn",s1[1]);//prints? e
?printf("ns1==%cn",s1);//may print some junk /? no values
}

  Was this answer useful?  Yes

int main()

{

char a[8]='e','x','f','o','r','s','y','s';

}

we cant initialize in the above way.

just consider the way we initialize array

main()
{
int a[3]={1,2,3};

}

In the same way we can Initialize string as

main()

{

char a[10]={'e','x','f','o','r','y','s'};

}

 

  Was this answer useful?  Yes

Abhi Phirke

  • Nov 19th, 2007
 

main()

{

char a[10]={'e','x','f','o','r','y','s', ''};

}


Aint this more correct? We are talking about strings here...!

 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions