String with variable size

This is a discussion on String with variable size within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I'm trying to write a program that has a string array with variable dimension - it reads the ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    114

    String with variable size

    Hi, everyone. I'm trying to write a program that has a string array with variable dimension - it reads the dimension from the keyboard, and, depending on the value, gives certain values to the array offsets. However, when I compile the code, I get the following error on the bold lines below: "error: incompatible types when assigning to type 'char[(unsigned int)(size + 1)]' from type 'char *'". The code is as follows:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    
        int size, i;
        char name[size + 1];
    
        printf("Choose a size: ");
        scanf("%d", size);
    
        if (size == 2)
        {
            name = "AB";
        }
        else if (size == 3)
        {
            name = "ABC";
        }
    
       //prints the string:
        for (i = 0 ; i < strlen(name) ; i++)
            printf("%c", name[i]);
    
        return 0;
    
    }
    How can I change the code so that it be correct? Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've gotten to here without learning about strcpy?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You would need to use C99, and you would have to declare name after you have called scanf, so that size actually has a valid value. Then you would need to use something like strcpy to copy the strings you want into name. You need to also make sure you know that strings must include room for the nul character, so if you want a string whose length is 2, then you need 3 spaces allocated for it.

    [edit] Alternately, you can just assign a bigger value to name, so it's bigger than the numbered options you are presenting, that way you know there is enough room to copy your letters in. [/edit]

    Or, since you are using string literals here, you could make name a pointer to a char, and just assign string literals the way you are, and not worry about any of that.


    Quzah.
    Last edited by quzah; 05-15-2011 at 06:49 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    quzah, thanks! But how do I change the code to follow your last suggestion?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you said you were new to programming (which I guess you went back and deleted), you would be better served learning about arrays before you start with pointers.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Ok, thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array size set by variable
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 11-20-2009, 03:13 PM
  2. variable size limits
    By rogster001 in forum C Programming
    Replies: 2
    Last Post: 09-01-2009, 07:51 AM
  3. Entering string variable size
    By Micko in forum C Programming
    Replies: 2
    Last Post: 01-18-2004, 09:29 PM
  4. entering string variable size
    By Micko in forum C Programming
    Replies: 6
    Last Post: 11-23-2003, 02:39 PM
  5. Variable Size
    By Gr3g in forum C++ Programming
    Replies: 13
    Last Post: 04-17-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21