Scanning multiple strings in one line to a pointers array in C

I’m trying to write a code for a function that takes an empty array of pointers and its length n.
And scans n strings of max length of MAX_LEN (a defined constant) until a new line is given by the user.
The strings are separated by one space.

int scan_strings(char * words(), int n) {
    char current = getchar(); // to make sure that there is no new line
    int i = 0, j = 0, on = 1;
    while (scanf("%c",&current) == 1 && (current != 'n' || on)) {
        char str(MAX_LEN); // a defined constant
        on = 0;
        if (current != ' ') {
            str(i) = current;
            i++;
        }
        else {
            str(i) = 0;
            i = 0;
            *(words+j) = &str;
            j++;
        }
    }
    return 1;
}

For some reason, the array after writing “foxes are red”
is just:

0x002b8f0c “red”
0x002b8f0c “red”
“ERROR READING CHARACTERS”
“ERROR READING CHARACTERS”
“ERROR READING CHARACTERS”

How can I fix this?

c++ – Can you define node pointers in a base binary tree class?

I’ve created two separate binary tree classes, with some shared functions/variables and some that are not shared. So I have tried to abstract away the similarities in a base BinaryTree class.

class BinaryTree{
public:
BinaryTree(std::vector<int> &vec);

BinaryTree *left;
BinaryTree *right;
std::vector<int> dataVector;
//functions...
}

The derived classes share the type of data with the base class (dataVector). But they require the creation of new nodes of their derived type.

class Derived : public BinaryTree{
public:
void function(){
left = new BinaryTree();
};

Obviously by creating a new BinaryTree, we lose the extended functionality of the Derived class, but if I were to declare left and right as Derived* inside the Derived class, I would defeat the whole purpose of abstraction in the first place.

How would I go about implementing this if it’s even possible?

Copy an array using pointers

Is this a correct way to copy elements from array origin to array location?

  #include <stdio.h>
   void copy(const int *origin, int *location, int n){
   int i;
   for(i=0;i<n;i++){
    location(i)=origin(i);
    }
  }

C Programming | Pointers

#include <stdio.h>

int main()
{
int a ()={10,20,30,40,50};
int *p=a, i=0;
for(i=0;i<=4;i++)
*++p=*p+10;
for(i=0;i<=4;i++)
printf("%d", a(i));
}

On a gcc compiler, I am getting its output as 10 20 30 40 50. Please tell me how?

game design – Pointers on making my first ever video

I have had a love for video games for years. Hack and slash, rpg, mmorpg, racing, first and thrid person shooters, and open world, story driven games. I’m looking to start in making my own and first ever game. I will be doing this by myself, on my own time, and my own money, so I’m doing this for mainly practice and honing my skills, so no big, overly ambitious ideas yet, but I’m looking for what would be a good program to start breaking ground in training myself in making video games and being able upload them.

hack and slash – Pointers for making my first video game

I have had a love for video games for years. Hack and slash, rpg, mmorpg, racing, first and thrid person shooters, and open world, story driven games. I’m looking to start in making my own and first ever game. I will be doing this by myself, on my own time, and my own money, so I’m doing this for mainly practice and honing my skills, so no big, overly ambitious ideas yet, but I’m looking for what would be a good program to start breaking ground in training myself in making video games and being able upload them.

c – Does it break strict aliasing rules if I point to byte arrays with packed struct pointers?

I’ve been familiarizing myself with C’s strict aliasing rules. I’ve looked at several examples of what not to do, and it always involves casting floats as ints, or casting structs to uint32_t pointers, which to me is obviously bad.

However, in my embedded code, I frequently need to work with CAN data buffers. These are 8 byte buffers, and in many cases the protocols and interfaces I’m working with pack fields together in their packets without any padding. So in order to keep the code easy to read and work with, I define the packet as a packed struct, and then typecast the CAN frame’s data buffer (a uint8_t(8)) to a struct pointer, and then modify the buffer by modifying the struct.

For example:

#define DATA_SIZE 8
struct can_frame{
    uint32_t canid;
    uint8_t dlc;
    uint8_t data(DATA_SIZE);
}

struct packet_example __attribute__((__packed__)) {
    uint8_t field1;
    uint32_t field2;
    uint16_t field3;
    uint8_t field4;
}
_Static_assert(sizeof(struct packet_example) == DATA_SIZE);

void prepare_packet()
{
    struct can_frame frame;
    struct packet_example * p = (struct packet_example *) &(frame.data(0));
    frame->field1 = 1;
    frame->field2 = 2;
    frame->field3 = 3;
    frame->field4 = 4;
}

Is this considered a valid use of C? Or should I stop doing this? Do I need to enable -fno-strict-aliasing? What are the implications of coding this way?

Compiling this kind of code with -Wall and -Werror doesnt give me any warnings and it proceeds fine. However, after reading about strict aliasing, I’m starting to worry that perhaps I’m doing something wrong that the compiler isn’t warning me about.

pointers – Pre increment operator and dereference operator resulting in segmentation fault, can’t seem to understand why

Found the following piece of code given in a test which asked to figure out the output.

#include <stdio.h>

int gate(char *P)
{
    char *q = P;
    q++;
    *q++;
    ++*q;
    return(q-P);
}


int main()
{
    char *s = "gateexam";
    int x = gate(s);
    printf("%d",x);
}

Ran it on an online compiler, but for some reason it is causing a segmentation fault because of the line “++*q” (commenting out this line makes the program run fine).

Can’t understand what is causing this

screenshot of code and output

c++ – How to use smart pointers with SDL2 (SDL_RWops)?

I want to write read savefile function with smart pointers and SDL2. I have little expirience with smart pointers and just want to ask is my code good, correct and no memory leaks.

this is my code:

std::shared_ptr<SDL_RWops> saveFile;

int GameEngine::ReadSave() {

        int Highlevel;

        //Open file for reading in binary
        saveFile = std::shared_ptr<SDL_RWops>(SDL_RWFromFile( "data/save.txt", "r" ), SDL_RWclose);

        //File does not exist
        if( saveFile.get()  == NULL ){
                printf( "Warning: Unable to open file! SDL Error: %sn", SDL_GetError() );
                
                Highlevel = 0;

                //Create file for writing
                saveFile = std::shared_ptr<SDL_RWops>(SDL_RWFromFile( "data/save.txt", "w+" ), SDL_RWclose);

                if( saveFile.get() != NULL ){
                        printf( "New save file created!n" );

                        //Initialize data
                        SDL_RWwrite( saveFile.get(), &Highlevel, sizeof(int), 1 );
                }
                else {
                        printf( "Error: Unable to create savefile! SDL Error: %sn", SDL_GetError() );
                }
        }
        else{
                //Load data
                printf( "Reading save file...!n" );

                SDL_RWread( saveFile.get(), &Highlevel, sizeof(int), 1 );
        }

        return Highlevel;
}

pointers – Buffer Overflow issue in C code

I’ve been writing the following bit of code, and someone had informed me that it runs of the risk of having a buffer overflow.

Now admittedly my knowledge of buffer overflows may be not be as robust as I would like, but I thought that a buffer overflow is when the data being written will not fit within the bounderies of the buffer and will spill over to adjacent memory location.

I assumed that the problem may be related to the fread, that it wouldn’t be a safe function to use, but reading through the documentation doesn’t seem to tell me that its unsafe like say strcpy() is compared to strncpy(). So I’m rather uncertain on where the problem could be located or how to handle it. And also if anyone has any suggestions on where I can go (or what book to read) that would help expand my knowledge on this subject or other vulnerability weaknesses, I would be appreciative.

bool readLong(FILE *f, long *n)
{
    unsigned char *ptr,tmp;

    if (fread(n,8,1,f) != 1)
        return false;

    ptr = (unsigned char *)n;
    tmp = ptr(0);
    ptr(0) = ptr(7);
    ptr(7) = tmp;
    tmp = ptr(1);
    ptr(1) = ptr(6);
    ptr(6) = tmp;
    tmp = ptr(2);
    ptr(2) = ptr(5);
    ptr(5) = tmp;
    tmp = ptr(3);
    ptr(3) = ptr(4);
    ptr(4) = tmp;

    return true;
}