0

I know I should not use c and c++ at the same time.

Can someone say why the above code are working using new? The purpose is to remove the central character of an word given by keyboard ex: "abcde" to "abde"

I was asking if the creation of VLA is correct or not... apparently it returns what I want BUT the same main code without the other functions crashes.

I searched throw internet and I discovered that I should initialize the size ('n' in my case) of the VLA's.

Code using functions and new:

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

int citirea_sirului(char *s1, char *s2)
{
    int d;
    printf("Cuvantul: ");
    gets(s1);
    d=strlen(s1);
    for(int i=0;i<d;i+=1)
    {
        *(s2+i)=*(s1+i);
    }
    return d;
}

void prelucrarea_afis_siruluiC(char *b, int d, char *a){
    strcpy(a,b+(d/2)+1);
    strcpy(b+(d/2),"");
    strcat(b,a);
    puts(b);
}

int main(){
    int n;
    char *cuv,*ccuv;
    cuv=new char[n];
    ccuv=new char[n];
    n=citirea_sirului(cuv,ccuv);
    printf("Dimensiunea Cuvantului: %d\n",n);
    printf("\nSir prelucrat: \n");
    prelucrarea_afis_siruluiC(ccuv,n,cuv);
    delete[] ccuv;
    delete[] cuv;
    return 0;
}

Code without functions:

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

int main(){
    int n;
    char cuv[n], ccuv[n];
    printf("Cuvantul: ");
    gets(cuv);
    n=strlen(cuv);
    printf("Dimensiunea Cuvantului: %d",n);
    for(int i=0;i<n;i++)
    {
        ccuv[i]=cuv[i];
    }
    strcpy(cuv,cuv+(n/2)+1);
    strcpy(ccuv+(n/2),"");
    strcat(ccuv,cuv);
    printf("\nCuvantul prelucrat: %s",ccuv);
    return 0;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ion Tudos
  • 1
  • 1
  • Another thing i created those code like 1 year ago and i remember at the time both worked fine. But now only the one with functions apparently works. – Ion Tudos Feb 05 '23 at 10:28
  • 1
    How big are the character arrays? `n` is uninitialised, so the size of the arrays could be anything. Maybe you just got lucky with one version... – Fe2O3 Feb 05 '23 at 10:38
  • the n un-initialized return, every time i run (the first main), 142 – Ion Tudos Feb 05 '23 at 10:44
  • so i put like a word longer than 142 and it works. But when i print the result word the characters after the one in 141 position are garbage characters – Ion Tudos Feb 05 '23 at 10:48
  • In both versions, `n` has no value when you define the arrays. The results will be *random*. – Weather Vane Feb 05 '23 at 10:48
  • Do not use uninitalised variables. Today you're lucky, tomorrow you're not lucky. Would you climb on a plane if the pilots weren't bothering to check how much fuel was in the tanks? – Fe2O3 Feb 05 '23 at 10:49
  • And using `gets()` only makes it worse. It is obsolete and is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Feb 05 '23 at 10:50
  • Ok i understand that i should initialize the size 'n'. But, at this point what should i do to correct my code in order to accommplish my goal? – Ion Tudos Feb 05 '23 at 10:59

0 Answers0