#char array declaration help

49 messages · Page 1 of 1 (latest)

native moss
#

I have this char array that will hold a string(name) but i want to make it where the size of that array is what the user inputs

    char src[MAX_LINE_LEN];
    printf("String Copy:\n");
     printf("Enter dest (string you want to copy to: ");
    scanf("%s", dest);
    int n = csc225_getline(dest, MAX_LINE_LEN);
    dest[] = dest[n]; ```
like for example this snippet of code
i have dest char array then i ask user to input a string for it
fickle perchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

native moss
#

then i use csc225_getLine which returns the number of characters written into dest including \0
and Im trying to declare the size of dest to the size of the string that the user inputted

scenic jackal
#

!vla

fast trellisBOT
#
What Is a VLA And Why Is It 'Bad'?

A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.

VLAs have poor compiler support and can lead to inefficient code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla (turned on by -Wall) it can be easy to create a VLA by accident, even in C++ with some compilers.

Compiler Support

✅ available since C99
⛔ not available in C++ at all
⛔ was never supported by MSVC
⚠ optional feature since C11
⚠ supported as non-standard extension by GCC, clang

scenic jackal
#

you should much rather use malloc/calloc

#

!man malloc

fast trellisBOT
#

malloc, free, calloc, realloc, reallocarray - allocate and free
dynamic memory

The malloc() function allocates size bytes and returns a pointer
to the allocated memory. The memory is not initialized. If size
is 0, then malloc() returns either NULL, or a unique pointer
value that can later be successfully passed to free().

Synopsis

#include <stdlib.h>

void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);
native moss
#

?

#

im sry i dont comprehend this

scenic jackal
#

To make it short: There is such a thing as dynamically sized arrays, but it's bad, don't use it.

native moss
#

so. how can i make the char array the size of the string that the user inputs

#

for example i want char dest[] to be [5] if user inputs abby but if they put another name like tom i want it to be size 4

scenic jackal
#

The cast is also unnecessary, but where I learnt it, it was considered good practice

native moss
#

do you know why my printstatement for before= wont print? https://onlinegdb.com/AEwAsxKBB

scenic jackal
native moss
#
 printf("Enter src (string you want to copy from): ");
    //scanf("%s", src);
    fgets(src, MAX_LINE_LEN, stdin);
    printf("before= %s\n", dest);```
#

i fixed it but still get the same thing

scenic jackal
#

Please read the first sentence of my last message again.

native moss
#

im a little slow sorry

#

but what are u trying to say? I cant print because the scanf right? but i just removed it for the fgets liek you said

scenic jackal
#

Ah sry, I overlooked the comment

#

What's it doing now that you replaced it for fgets?

#

It might be that the first scanf still has something to do with this

#

so change that to an fgets, too

native moss
#

gives me the same thing

scenic jackal
#

Let me guess, you're using scanf before that already...

native moss
#

ok

scenic jackal
#

it's really incredibly helpful, not only for user I/O, but also for general C understanding

native moss
#

yea i got it

scenic jackal
#

and it's not that long nor difficult

native moss
#

so scanf is a no go when taking user input?

#

yea thanks for the links

scenic jackal
#

If you just want a quick testing sesssion, scanf is fine, but it can lead to problems really fast, if you don't know exactly what you're doing

native moss
#

ill check em out im just on a time crunch w this project and prof didnt give us a long deadline

scenic jackal
native moss
#

ok im trying to run my main function in linux but im getting these errors do you know why that is?

#

because in the online compiler i had no errors in my program and it ran fine

scenic jackal
#

They're just warnings, but apparently you're not using the return value, which returns either the first parameter again or NULL in case of an error

#

I've never seen a compiler complain about that though

native moss
#

!solved

fickle perchBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] char array declaration help

fickle perchBOT
#

char array declaration help