Question

The code I have is quite simple in one method I have this:

// This line has an Intellisense Error: Initialization with {...} expected for aggregate object
char str[] = GetBuffer(); // x 64 will give us 512 (sector sized buffer) ;

The GetBuffer metod is this:

char * GetBuffer(void)
{
int idx = 0;
int offset = 0;
char *buffer[512];

for(idx =0; idx < 64; idx ++)
{
            // This line has an Itellisense Error: "Expected Expression"
    buffer[offset + idx] = {"E","R","A","S","E","D"," ", " "};

    offset += 8;
}

return *buffer;
}

Any ideas what's wrong with this? All I am trying to do - is populate a buffer with 512 bytes which contain the following string repeated: "ERASED " This is ANSI C (not C++) and it has been so long since I coded in ANSI C - please help and be kind!

Using Visual Studio 2012

EDIT 1 Ok lots of things have been fixed thanks to you guys - but no full answer yet. The str buffer holds 528 characters and not 512 and contains a lot of ERASED as expected but ends with

ýýýý««««««««îþîþ

Any ideas with this? And Oh boy I have a great deal of pure C reading to do - I have forgotten way too much!

Was it helpful?

Solution 2

there is problem with your buffer creation. you'd malloc such that it's not reclaimed by the function invoke routine. Second, you can't do assignment like the line you encountered a Itellisense error. You can use this:

#include "stdlib.h"

char * GetBuffer(void)
{
    int i = 0, idx = 0;
    const char * cstr_init = "ERASED  ";
    char *buffer = (char*)malloc(512);

    for (idx = 0; idx < 512; idx+=8) {
        for (i = 0; i < 8; i++) {
            buffer[idx+i] = cstr_init[i];
        }
    }

    return buffer;
}

OTHER TIPS

You can't initialize an array with the return value from a function.

You could use a pointer instead of an array:

char *str = GetBuffer();

Or you could use strcpy() or a relative — but there are buffer overflow risks:

char str[512];

strcpy(str, GetBuffer());

Your GetBuffer() function also has a lot of problems.

char *GetBuffer(void)
{
    int idx = 0;
    int offset = 0;
    char *buffer[512];

This should probably be char buffer[512];, but...

    for(idx =0; idx < 64; idx ++)
    {
                // This line has an Itellisense Error: "Expected Expression"
        buffer[offset + idx] = {"E","R","A","S","E","D"," ", " "};

You can't set arrays like this. And you needed double quotes because of the char *buffer[512] problem.

        offset += 8;
    }

    return *buffer;
}

And you should not return a local variable — it is destroyed when the function returns so it can't be used afterwards.

You might write:

char *GetBuffer(void)
{
    char *buffer = malloc(257);
    if (buffer != 0)
    {
        int idx;
        for (idx = 0; idx < 256; idx += 8)
            strcpy(buffer+idx, "ERASED  ");
    }
    return buffer;
}

There's a small layer of obfuscation going on with the hard-coded lengths and limits; they're correct, but the interconnections between the sizes are not obvious — and ideally, they should be:

  • strlen("ERASED ") == 8
  • 256 = 32 * strlen("ERASED ")
  • 257 = 32 * strlen("ERASED ") + 1 (the one is for the terminal null)

And then the calling code might be:

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

int main(void)
{
    char *str = GetBuffer();

    if (str != 0)
    {
        printf("<<%s>>\n", str);
        free(str);
    }

    return(0);
}

There are several things wrong here.

In C, a character array can be initialized with an initializer list or a string literal. You cannot use the return value from a function to initialize the array. So

 char str[] = GetBuffer(); 

will not work.

Also, char* buffer [512] is an array of 512 pointers to char, i.e., an array of 512 strings. buffer [offset + idx] would be one pointer to char. It can hold only one string, but you are trying to assign eight strings to it: "E", "R", etc. If you mean those to be chars and not strings, use single quotes: 'E', etc. However, even that won't work unless you allocate memory to the pointer so that it can hold the string.

As written, the array of pointers is allocated on the stack, so it goes out of scope when the function terminates. return *buffer would return the first string in the array of strings, but that's a local variable, so you're returning the dereferenced value of a pointer that is no longer in scope.

I think a simpler way to accomplish your goal is this:

char str [512] = {'\0'};

for (int i = 0; i < 511; i += 7)
    strcat (str + i, "ERASED ");

It's not very general, but it does what you want.

Edited to reflect Jonathan Leffler's comment that strcat (str, "ERASED "), which is what I originally had, is inefficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top