Thread: Declare string of dynamic length in C

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    2

    Declare string of dynamic length in C

    I have the following declaration:
    Code:
     char name[size + 1] = {'\0'};  
    where size has a predefined value. I don't want to fix the size of the Array. I want to know how to declare a string which will hold a name with dynamic lenth in C.

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When do you know the length of the string you want to store?
    At some point beforehand, you need to know the length, be it
    size_t size = strlen(s);
    char name[size+1];
    strcpy(name,s);

    or
    size_t size = strlen(s);
    char *name = malloc(size+1);
    strcpy(name,s);


    What is the lifetime of the string once you've stored it?
    Your VLA cannot be returned from the current scope for example.
    Also, VLA's can blow the stack without warning, whereas malloc will always return a status that allows you to recover gracefully.

    There is no such thing in C as being able to declare
    string name;
    and then be able to retrieve arbitrary length input from some source without any further work on your part.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    I have a variable 'typeName' and a variable 'Name'. The varaibale 'Name' stores a string of characters in the following Format: Auto_'typeName'_Workflow. The string 'typeName' is determined at run time.
    Code:
    char built_name[name_size + 1] = {'\0'};
    sprintf(name, "Auto_%s_Workflow", typeName);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So name_size is just
    strlen(typeName) + strlen("Auto__Workflow");

    You could use the non-standard function asprintf(3): print to allocated string - Linux man page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why declare the length of arrays in c?
    By johnhoffman in forum C Programming
    Replies: 2
    Last Post: 02-04-2012, 08:51 PM
  2. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  3. length of dynamic array
    By budala in forum C Programming
    Replies: 12
    Last Post: 02-12-2011, 02:25 PM
  4. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  5. how do you declare a string?
    By gcn_zelda in forum C++ Programming
    Replies: 16
    Last Post: 04-10-2003, 04:15 AM

Tags for this Thread