Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to store multiple characters in a char variable?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ihsh




PostPosted: Fri Aug 27, 2010 9:04 pm   Post subject: How to store multiple characters in a char variable?

code:
#include  <stdio.h>

void main()
{
        char a [20];
        a="hello";
        printf("%s,a);
}


This (a simplified version) was what I was trying to do and I keep getting the "LValue" error.
How can you store multiple characters in a char variable without using user input?

Thank you. Smile
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Fri Aug 27, 2010 9:37 pm   Post subject: RE:How to store multiple characters in a char variable?

Theres two ways. First you can declare your array with indeterminate size and assign the string literal to the array as its declared.

code:

#include <stdio.h>

int main()
{
    char a[] = "hello";

    printf("%s", a);

    return 0;
}



The other way is to use pointers. Since the string literal must already be in memory when we run the application, we can just point to it.

code:

#include <stdio.h>

int main()
{
    char *a;
    a = "hello";

    printf("%s", a);

    return 0;
}


This will allow you to access a string of any size, and you will be able to assign any string to it even after declaring it. But since you are pointing to a string literal, which is constant, you won't be able to edit it or you will get a segfault.

I tested this in C++ but AFAIK there shouldn't be any difference.

Hope that helps.
michaelp




PostPosted: Fri Aug 27, 2010 9:40 pm   Post subject: Re: How to store multiple characters in a char variable?

You need to use the function "strcpy" from string.h:

code:

#include <stdio.h>
#include <string.h>

int main()
{
    char s[20];
    strcpy(s, "Hello!" );
    printf( "%s\n", s );
    return 0;
}


Some other differences from your code:
1. Your main function returns 'void'. This is bad! Here is one of many articles that help explain why that is bad:
http://users.aber.ac.uk/auj/voidmain.shtml
(Many more can be found by Google.)
It is non standard, and can cause problems because the OS usually expects main to return an integer. If you compile code that uses "void main()" in it with warnings, you will see that you get a warning about it.
Use 'int main()' and have 'return 0;' at the end of your main function.
2. I added '\n' after the string format specifier '%s'. This is called an escape sequence, and when printed, it is a newline, making what is being printed go onto the next line.
3. You forgot to close the quote after the '%s' when you used printf.

Read more about strcpy:
http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
About strings in general:
http://www.cprogramming.com/tutorial/c/lesson9.html
DtY




PostPosted: Sat Aug 28, 2010 7:46 am   Post subject: Re: How to store multiple characters in a char variable?

michaelp @ Fri Aug 27, 2010 9:40 pm wrote:
1. Your main function returns 'void'. This is bad! Here is one of many articles that help explain why that is bad:
http://users.aber.ac.uk/auj/voidmain.shtml
(Many more can be found by Google.)
It is non standard, and can cause problems because the OS usually expects main to return an integer. If you compile code that uses "void main()" in it with warnings, you will see that you get a warning about it.
Use 'int main()' and have 'return 0;' at the end of your main function.
The information on the website looks pretty archaic. As far as I'm concerned, because the standard says so is a good enough reason to do it, HOWEVER, in any modern compiler, the program will return an exit status of zero if main is declared to return void, I don't think this has really been an issue for a while.
ihsh




PostPosted: Sat Aug 28, 2010 9:07 am   Post subject: RE:How to store multiple characters in a char variable?

Thank you very much for your replies! Smile
How can I change the status to "Answered?"
TheGuardian001




PostPosted: Sat Aug 28, 2010 5:05 pm   Post subject: Re: How to store multiple characters in a char variable?

The same way you set it to "hello."

Either by strcpy if you are using char[], or by = if you are using char*, IE:

code:

#include <stdio.h>
#include <string.h>
int main()
{
    //Using Character array
    char str1[20];
    strcpy (str1, "Answer");
    printf("%s\n",str1);
    strcpy (str1, "New Value");
    printf("%s\n", str1);


    //Using char* (does not require string.h)
    char* str2;
    str2 = "Answer";
    printf("%s\n", str2);
    str2 = "New Value";
    printf("%s\n", str2);

    system("PAUSE");
    return 0;
}
DtY




PostPosted: Sat Aug 28, 2010 7:02 pm   Post subject: Re: RE:How to store multiple characters in a char variable?

ihsh @ Sat Aug 28, 2010 9:07 am wrote:
Thank you very much for your replies! Smile
How can I change the status to "Answered?"
Just by posting, there's nothing special to do on here.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: