Character Arrays Subscripting

Pages: 12
is there anyway i can incorporate the use of the " & " (ampersand) to better my program?
1
2
3
   int value2=(value1+v+2);

    char stringy[value2];


This is illegal in C++. You might want to turn off the compiler extension that allows it. Array sizes must be compile time constants.

Apparently I can assign the value of the new array to array1 by doing

string1=nstring;

No, you can't. That sets the local variable string1 to point to nstring. It does not change the c-string fed to the function.

The way strcat is used in C, the caller must ensure that the first parameter points to enough memory to hold both the first string and the string to be concatenated. I would expect an implementation to look something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char* mystrcat ( char* destination, const char* source )
{
    char* dst = destination ;
    while ( *dst )
        ++dst ;

    while ( (*dst++ = *source++) != '\0' )
        ;

    return destination ;
}

// or, if you've already defined a mystrcpy/mystrlen:

char* mystrcat ( char* destination, const char* source )
{
    mystrcpy(destination+mystrlen(destination), source) ;
    return destination ;
}
Last edited on
ok, my bad for misinteterpreting at the time. And I get what you're saying since the array name points to the start of the array I was simply assigning it to point to another location.
Topic archived. No new replies allowed.
Pages: 12