Thread: String, Chars, and Arrays Oh my (confusion)

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    16

    String, Chars, and Arrays Oh my (confusion)

    I am writing some code and I am getting stuck, here is the snippet.
    Code:
    int j = 1;
    char r[7];
    
    if(j=1)
    r = "Degrees";
    else
    r = "Radians";
    
    printf("Please enter the angle in %s: \n", r);
    When the print out comes around it just prints out the ASC. I forget how to make it so it will print out either Degrees and Radians. Also...

    I am trying to create a array that has undeclared length. I was wondering how I would go about doing so. Such as
    Code:
     int x [];
    printf("How big should the array be?");
    From here I want to be able to control the length of the array. Can anyone help with these two problems I am having.. I havnt programmed in C in a long time!

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    (1) the array must be large enough to hold the string you want to copy AND a '\0', so the size of r should be 8 or more.
    (2) instead of doing this
    Code:
    r = "Degrees";
    you should do this
    Code:
    strcpy(r, "Degrees");

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, if(j=1) doesn't do what you think it does (hint: what would you say j=1 does if you saw it by itself?) To get an "array" of user-given length, you would need malloc.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also - you do not need array here - you need only pointer

    Code:
    int j = 1;
    const char* r;
    
    if(j==1)
       r = "Degrees";
    else
       r = "Radians";
    
    printf("Please enter the angle in %s: \n", r);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Toonzaka View Post
    I am trying to create a array that has undeclared length. I was wondering how I would go about doing so. Such as
    Code:
     int x [];
    printf("How big should the array be?");
    From here I want to be able to control the length of the array. Can anyone help with these two problems I am having.. I havnt programmed in C in a long time!
    You cannot do that.
    Two solutions to that one, though.
    First is: ask for length first, then create the array: char myarr[length]; (Requires C99)
    Second is dynamic memory: ask for length, then use malloc and free (Works with C89).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A lottery program - arrays, ints, chars etc
    By Glauber in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2008, 10:48 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Problem with chars and char arrays
    By Zagaberoo in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 08:47 AM
  5. String array's? How can you???
    By Nuke in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-13-2003, 03:41 PM