c++ – warning: ‘struct task_struct’ “Declared inside parameter list will not be visible outside of this definition or declaration”

I have the following piece of code:

File sched.h

   #include <stats.h>
   truct task_struct {
       ...
       struct stats stat;
   };

File stats.h*

   #include <sched.h>
   struct stats
   {
   ...
   };

   void initStats (struct task_struct* tsk);

When I try to compile it gives me the following warning

warning: ‘struct task_struct’ declared inside parameter list will not be visible outside of this definition or declaration
17 | void initStats (struct task_struct* tsk);

I have seen similar questions posted, but I have not been able to fix the problem. I was wondering if the problem was because both files include each other. Any help would be appreciated : ).

c++ – A program that reads from file And saves to the class and to in class struct

So i wrote this program that is suposed to save lines from file to class, scheme is 4lines x * 5lines ; 4lines x * 5lines etc. But it won’t finish even first loop i dont know what is wrong with it. I am super sorry for variable names. and yes i know this code is supper shitty and i can be done easier but i dont know any other way to do this.
Here is file example:

Pracownik_1
10
luty
2021
Jabłka01
1.80
2
0
3.60
Cebula_Czerwona
1.70
10
0
17.00
20.60
;
Pracownik_2
12
luty
2021
Cebula_Czerwona
1.70
10
10
17.00
15.30
;

and code:

#include <iostream>
#include <fstream>

using namespace std;

class DokumentySprzedazoweKlasa
{
    public:

    string IdPracownika;

    string sprawdzaczsrednik;
    string Dzien;
    string Miesiac;
    string Rok;
    string SumaParagonuTymczasowa;
    float SumaParagonu;
    struct Sprzedaz
    {
        string NazwaProduktu1;
        float CenaJednostkowa;
        unsigned int IloscSztuk;
        unsigned int Rabat;
        float Cena;
        string CenaJednostkowaTymczasowa;
        string IloscSztukTymczasowa;
        string RabatTymczasowa;
        string CenaTymczasowa;
        string skipper;
    }Sprzedaz1(10);

    void OdczytywanieDokumentySprzedazowe(DokumentySprzedazoweKlasa dsk(10))
    {

        int j=0;
        ifstream wejsciedokumentysprzedazowe("DokumentySprzedazowe.txt");
        for(int i=0;!wejsciedokumentysprzedazowe.eof();i++)
        {
                    getline(wejsciedokumentysprzedazowe,dsk(i).IdPracownika);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Dzien);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Miesiac);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Rok);
                while(dsk(i).Sprzedaz1(j).skipper==";")
                {
                    if(!dsk(i).Sprzedaz1(j).NazwaProduktu1.empty())
                    {
                    getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).NazwaProduktu1);
                    }
                    getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).CenaJednostkowaTymczasowa);
                    dsk(i).Sprzedaz1(j).CenaJednostkowa=stof(dsk(i).Sprzedaz1(j).CenaJednostkowaTymczasowa);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).IloscSztukTymczasowa);
                    dsk(i).Sprzedaz1(j).IloscSztuk=stoul(dsk(i).Sprzedaz1(j).IloscSztukTymczasowa);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).RabatTymczasowa);
                    dsk(i).Sprzedaz1(j).Rabat=stoul(dsk(i).Sprzedaz1(j).RabatTymczasowa);
                    getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).CenaTymczasowa);
                    dsk(i).Sprzedaz1(j).Cena=stoul(dsk(i).Sprzedaz1(j).CenaTymczasowa);
                    getline(wejsciedokumentysprzedazowe,dsk(i).sprawdzaczsrednik);
                    if(isdigit(sprawdzaczsrednik(0))==1)
                    {
                        dsk(i).SumaParagonuTymczasowa=dsk(i).sprawdzaczsrednik;
                        dsk(i).SumaParagonu=stof(dsk(i).SumaParagonuTymczasowa);
                        i++;
                         cout<<"pierwszy if";
                    }
                    else
                    {
                        j++;
                        dsk(i).Sprzedaz1(j).NazwaProduktu1=dsk(i).sprawdzaczsrednik;
                        getline(wejsciedokumentysprzedazowe,dsk(i).Sprzedaz1(j).skipper);
                        cout<<"drugi if";
                    }
                }

            }
        wejsciedokumentysprzedazowe.close();
    }

};


int main()
{
    DokumentySprzedazoweKlasa DokumentySprzedazoweKlasa1(10);
    DokumentySprzedazoweKlasa1(10).OdczytywanieDokumentySprzedazowe(DokumentySprzedazoweKlasa1);

    return 0;
}

```

c++ – How to get struct layouts from vertex shader input using ID3D12Reflection

I am attempting to write a code generator that builds C++ structs/classes from D3D12 reflection data built using DXC. Given a simple vertex shader signature:

struct Light
{
    float3 Color;
    float3 Dir;
};

struct cbPerPass : register(b0)
{
    float4x4 gWorldProjTransform;
    Light gGlobaLight;
};

struct VertexIn
{
    float3 Pos      : POSITION;
    float4 Color    : COLOR;
};

struct VertexOut;  // unimportant for the question

VertexOut VS(VertexIn vin) { // do calculations here }

I can retrieve the input parameters via ID3D12ShaderReflection::GetInputParameterDesc(), but this only reports the mapping of VertexIn members to shader registers and any attached semantics. I can get the layout of cbPerPass through ID3D12ShaderReflection::GetConstantBufferByIndex() and the layout of Light through the ID3D12ShaderReflectionType interface.

However, what I would like to retrieve is the layout of the VertexIn struct. Is there a way to do this?

go – JSON convert arbitrary struct to another struct

I’m new to Go and here’s some code that converts one struct to another using the json package. My inspiration for this code is I want to separate domain knowledge between the front-end and back-end.

For example, a client hits a RESTful endpoint that will return a JSON-encoded Frontend response. A database search produces a Backend object. I want to return the database result by converting the Backend object to a Frontend object and then have the endpoint return the Frontend response.

In the example below I have a simple example of just the conversion piece. Is what I’m doing brilliant or horrible?

package main

import (
    "encoding/json"
    "fmt"
)

type Backend struct {
    ID string `json:"id,omitempty"`
    Name string `json:"name,omitempty"`
    Value int `json:"value,omitempty"`
}

func (b *Backend) String() string {
    return fmt.Sprintf("%+v", *b)
}

type Frontend struct {
    ErrorCode string `json:"error_code,omitempty"`
    Success bool `json:"success,omitempty"`
    Name string `json:"name,omitempty"`
    Value int `json:"value,omitempty"`
}

func (f *Frontend) String() string {
    return fmt.Sprintf("%+v", *f)
}

func convert(from interface{}, to interface{}) {
    fmt.Println("From:", from)
    bytes, err := json.Marshal(from)
    if err != nil {
        panic(err)
    }
    fmt.Println("To (before):", to)
    err = json.Unmarshal(bytes, to)
    if err != nil {
        panic(err)
    }
    fmt.Println("To (after):", to)
}

func main() {
    from := Backend{
        ID:    "theID",
        Name:  "theName",
        Value: 42,
    }
    to := Frontend{
        ErrorCode: "theErrorCode",
        Success: true,
    }
    convert(from, &to)
    fmt.Println("To (finally):", to)
}

Result:

From: {theID theName 42}
To (before): {ErrorCode:theErrorCode Success:true Name: Value:0}
To (after): {ErrorCode:theErrorCode Success:true Name:theName Value:42}
To (finally): {theErrorCode true theName 42}

unreal 4 – Cannot create EAttachmentRule variable in a Blueprint struct

I’m trying to add a EAttachmentRule variable in a struct but the enum doesn’t appear in the list of available types

enter image description here

I managed to create a variable in a BP by dragging the pin from an AttachActorToComponent node and promoting it to a variable. But I can’t do the same thing to create a variable in a struct

c++ – Binary IO inspired by struct pack

I’m writing a header-only template library for helping with binary IO. It is inspired by pack-d, which is in turn inspired by Python’s struct module. I’ve only finished the output part and posted it here for review. You can find the source (~300 lines of code) at the bottom of the post.

It’s basically like a printf for binary IO and packs its arguments into a byte stream depending on the format string. The stream can be a memory stream backed by a std::vector of bytes, or represent packing directly to a file, however you wish. It supports extension for user types.

For implementation I’ve used c++20 and modern C++ features (such as concepts and constexpr if). The code compiles without warnings using g++ 10.2 and clang++ 11 with flags -std=c++20 -Wall -Wpedantic -Wextra. There are no dependencies outside of the standard library.

I’m writing it primarily for learning purposes, and for myself to use; though I’d be happy to publish it if others find it useful. I like this interface, and I didn’t come across anything similar for C++ except this which is for C, and uses C-style variadics instead of variadic templates, and I don’t think it’s extendable for user types.

I am interested in any constructive criticism or opinion you have to offer. The code works for the simple cases I’ve thrown at it, but it is possible it is buggy for complicated ones. Also one of my goals is for the code generated by template expansion to be as close as possible performance-wise to hand-written code based on fwrite. I’m also interested if any other modern C++ features would fit this kind of thing better than the way I’ve chosen to implement it.

// 6 byte string followed by null-byte
ctx.packf("6Bx", "Hello!"); // --> "Hello"

// You can control endianness
// (also whitespace in the format string is ignored)
ctx.packf(">I 3x <I", 0xdeadbeef, 0xbaadf00d); // --> 0x deadbeef 00 00 00 0df0adba

// You can pack structs raw
struct S {
    uint8_t  x;
    uint32_t y;
};

// Pad bytes are packed, and endianness option has no effect
ctx.packf(">r", S{ 0xff, 0xbaadc0de }); // --> 0x ff __ __ __ de c0 ad ba

// But you can also extend the system by specializing pack_custom
void pack_custom(auto& ctx, const S& s) {
    ctx.packf("BI", s.x, s.y);
}

// And then use generic packing
ctx.packf(">g", S{ 0xff, 0xbaadc0de }); // --> 0x ff ba ad c0 de

// Arrays are supported, by supplying either a constant width (see first example)
// or passing a width as a parameter
std::vector<S> v{ S{ 0xaa, 0xbaadc0de }, S{ 0xbb, 0xdeadbeef } };

ctx.packf(">*g", v.size(), v.data()); // --> 0x aa baadc0de bb deadbeef

// Or you can just specialize pack_custom for vectors as well
void pack_custom(auto& ctx, const std::vector<auto>& v) {
    // First pack the size as a 32-bit int, then the data generically
    ctx.packf("I*g", v.size(), v.size(), v.data());
}

// Then just call
ctx.packf(">g", v); // --> 0x 00000002 aa baadc0de bb deadbeef
ctx.packf("<g", v); // --> 0x 02000000 aa dec0adba bb efbeadde

// For basic types, "g" defaults to infering the type
// and packing as you'd expect for arithmetic types
// and packing raw for structs that don't have pack_custom
uint16_t a = 0xdead;
uint16_t b = 0xbeef;
ctx.packf("<gg", a, b); // --> 0x adde efbe

// And if all your format specifier are generic you can drop the format string
ctx.packf("gg", a, b)
// is the same as
ctx.pack(a, b)
// as well as this (you have to cast the literals in this case though)
ctx.pack((uint16_t)0xdead, (uint16_t)0xbeef)

// There's also  ctx.packle(...)  and  ctx.packbe(...)  for specifying endianness
// without the use of a format string.

The user needs to have a stream type that implements a fwrite-style method. A context is then created to wrap this stream type. All the packing functions are methods of the context object.

struct File_Stream {
    FILE *fp;

    size_t write(const void *ptr, size_t size, size_t nmemb) {
        return fwrite(ptr, size, nmemb, fp);
    }
};


FILE *fp = fopen("test.bin", "wb");
pack::Context ctx(File_Stream{fp});
// Use pack functions here
// ...
fclose(fp);

The standard packing for arithmetic types is a case like packf("4B", b), and the meaning depends on the type of b.

  • If b is a uint8_t or convertible to uint8_t, it means pack b (converted if necessary to uint8_t), repeating it 4 times.
  • If b is a pointer to a uint8_t or to a type convertible to uint8_t it treats b as an array and packs 4 of its elements (converted if necessary to uint8_t)

Both cases are handled by the function pack_helper<TPack_As, T>, where TPack_As depends on the format specifier (in this case it’s uint8_t because of the B), and T is whatever the type of b is.

If necessary, standard packing will also do endian conversion.

The function pack_raw handles the packing of raw data, and the function pack_generic decides which packing mode to use (custom, raw, or standard) in the generic case.

Whenever possible, if no endian conversion is necessary for example, the implementation tries to pack arrays of data to the stream all at once instead of one at a time (see the implementation of pack_array_endian).

The error handling is a WIP. For now I just throw runtime_error or logic_error whenever an error occurs. I plan to create my own exception classes which will also carry context information (such as how many bytes were packed when the error occured, and how many parameters were parsed).

I am also considering implementing convenience functions such as:

               void fpackf(FILE *fp, const char *format, const auto& value, auto... args);
    vector<uint8_t> vpackf(          const char *format, const auto& value, auto... args);

which wrap context creation for typical usage.

#pragma once
#include <type_traits>
#include <concepts>
#include <stdexcept>
#include <bit>
#include <string>

namespace pack {

using std::runtime_error;
using std::logic_error;
using std::convertible_to;
using std::same_as;
using std::remove_reference_t;
using Endian = std::endian;

// Utility concepts
template <typename T> concept Pointer    = std::is_pointer<T>::value;
template <typename T> concept Array      = std::is_array<T>::value;
template <typename T> concept Arithmetic = std::is_arithmetic<T>::value;

// Pack requires a fwrite-like interface
template <typename TStream>
concept Stream = requires(TStream stream, const void *ptr, size_t size, size_t nmemb) {
    { stream.write(ptr, size, nmemb) } -> convertible_to<size_t>;
};

// The extension mechanism
template <typename TContext, typename T>
concept Custom_Packable = requires(TContext& ctx, const T& a) {
    { pack_custom(ctx, a) };
};

template <Stream TStream>
struct Context {
private:
    TStream& stream;
    Endian   endian = Endian::native;
    size_t   len    = 1; // Repetition/array count

public:
    Context(TStream& s): stream(s) {}

private:
    void skip() {
        uint8_t x = 0;
        for (size_t i=0; i<len; ++i) {
            stream.write(&x, 1, sizeof x);
        }
    }

    void pack_value(const auto& value) {
        size_t n = stream.write(&value, sizeof value, 1);
        if (n != 1) {
            throw runtime_error("Failed writing to stream.");
        }
    }

    void pack_array(const auto& array) {
        size_t n = stream.write(array, sizeof array(0), len);
        if (n != len) {
            throw runtime_error("Failed writing to stream.");
        }
    }

    // Just packs raw data.
    // Will not consider pad bytes inside structs or endianness conversions.
    template <typename T>
    void pack_raw(const T& data) {
        if constexpr (Pointer<T> || Array<T>) {
            pack_array(data);
        } else {
            pack_value(data);
        }
    }

    template <Arithmetic TFrom, Arithmetic TTo>
    TTo transmute(TFrom x) {
        return *(reinterpret_cast<TTo*>(&x));
    }

    template <Arithmetic T>
    void pack_value_endian(const T& value) {
        if (endian != Endian::native) {
            if constexpr (sizeof(T) == 1) {
                pack_value(value);
            } else if constexpr (sizeof(T) == 2) {
                pack_value(__builtin_bswap16(transmute<T,uint16_t>(value)));
            } else if constexpr (sizeof(T) == 4) {
                pack_value(__builtin_bswap32(transmute<T,uint32_t>(value)));
            } else if constexpr (sizeof(T) == 8) {
                pack_value(__builtin_bswap64(transmute<T,uint64_t>(value)));
            } else {
                throw logic_error("Cannot perform endian conversion on this type.");
            }
        } else {
            pack_value(value);
        }
    }

    template <typename T>
    void pack_array_endian(const T& array) {
        if (endian != Endian::native) {
            for (size_t i=0; i<len; ++i) {
                pack_value_endian(array(i));
            }
        } else {
            pack_array(array);
        }
    }

    template<Arithmetic TPack_As, typename T>
    void pack_helper(const T& data) {
        if constexpr (convertible_to<T, TPack_As>) {
            for (size_t i=0; i<len; ++i) {
                pack_value_endian(static_cast<TPack_As>(data));
            }
        } else if constexpr (same_as<T, TPack_As*> || same_as<T, TPack_As()>) {
            pack_array_endian(data);
        } else if constexpr (Pointer<T> || Array<T>) {
            using TElem = decltype(data(0));
            if constexpr (convertible_to<TElem, TPack_As>) {
                for (size_t i=0; i<len; ++i) {
                    pack_value_endian(static_cast<TPack_As>(data(i)));
                }
            } else {
                throw logic_error("Type not convertible.");
            }
        } else {
            throw logic_error("Type not convertible.");
        }
    }

    // Decides whether to use pack_raw, pack_helper or pack_custom
    template <typename T>
    void pack_generic(const T& data) {
        using TContext = remove_reference_t<decltype(*this)>;
        if constexpr (Arithmetic<T>) {
            pack_helper<T>(data);
        } else if constexpr (Custom_Packable<TContext, T>) {
            for (size_t i=0; i<len; ++i) {
                size_t len_save    = len;     // Save state prior to pack_custom call
                Endian endian_save = endian;
                len = 1;                      // Reset len to 1 for pack_custom
                pack_custom(*this, data);
                len    = len_save;            // Restore state prior to pack_custom call
                endian = endian_save;
            }
        } else if constexpr (Pointer<T> || Array<T>) {
            using TElem = remove_reference_t<decltype(data(0))>;
            if constexpr (Arithmetic<T>) {
                pack_helper<TElem>(data);
            } else if constexpr (Custom_Packable<TContext, TElem>) {
                for (size_t i=0; i<len; ++i) {
                    size_t len_save    = len;     // Save state
                    Endian endian_save = endian;
                    len = 1;                      // Reset len
                    pack_custom(*this, data(i));
                    len    = len_save;            // Restore state
                    endian = endian_save;
                }
            } else {
                pack_raw(data);
            }
        } else {
            pack_raw(data);
        }
    }

public:
    void pack() {}

    void pack(const auto& value, auto... args) {
        pack_generic(value);
        pack(args...);
    }

    void packle(const auto& value, auto... args) {
        endian = Endian::little;
        pack_generic(value);
        pack(args...);
    }

    void packbe(const auto& value, auto... args) {
        endian = Endian::big;
        pack_generic(value);
        pack(args...);
    }

    void packne(const auto& value, auto... args) {
        if (Endian::native == Endian::little) {
            endian = Endian::big;
        } else {
            endian = Endian::little;
        }

        pack_generic(value);
        pack(args...);
    }

    void packf(const char *format) {
        while (*format) {
            switch (*format) {
                case '>': endian = Endian::big;    format++; continue;
                case '<': endian = Endian::little; format++; continue;
                case '=': endian = Endian::native; format++; continue;
                case '!': endian = (Endian::native == Endian::little)
                              ? Endian::big
                              : Endian::little;
                          format++; continue;
                case '0': case '1': case '2':
                case '3': case '4': case '5':
                case '6': case '7': case '8':
                case '9': {
                    char *endptr;
                    len = static_cast<size_t>( strtol(format, &endptr, 10) );
                    format = endptr;
                    continue;
                }
                case ' ': case 't': case 'n': ++format; continue;
                case 'B': throw logic_error("Not enough arguments for format string.");
                case 'H': throw logic_error("Not enough arguments for format string.");
                case 'I': throw logic_error("Not enough arguments for format string.");
                case 'L': throw logic_error("Not enough arguments for format string.");
                case 'b': throw logic_error("Not enough arguments for format string.");
                case 'h': throw logic_error("Not enough arguments for format string.");
                case 'i': throw logic_error("Not enough arguments for format string.");
                case 'l': throw logic_error("Not enough arguments for format string.");
                case 'f': throw logic_error("Not enough arguments for format string.");
                case 'd': throw logic_error("Not enough arguments for format string.");
                case 'r': throw logic_error("Not enough arguments for format string.");
                case 'g': throw logic_error("Not enough arguments for format string.");
                case '*': throw logic_error("Not enough arguments for format string.");
                case 'x': skip(); break;
                default:
                    throw logic_error("Unexpected character (" + std::string{*format} + ") in format string.");
            }
            len = 1;
            packf(++format);
            return;
        }
    }

    void packf(const char *format, const auto& value, auto... args) {
        while (*format) {
            switch (*format) {
                case '>': endian = Endian::big;    format++; continue;
                case '<': endian = Endian::little; format++; continue;
                case '=': endian = Endian::native; format++; continue;
                case '!': endian = (Endian::native == Endian::little)
                              ? Endian::big
                              : Endian::little;
                          format++; continue;
                case '0': case '1': case '2':
                case '3': case '4': case '5':
                case '6': case '7': case '8':
                case '9': {
                    char *endptr;
                    len = static_cast<size_t>( strtol(format, &endptr, 10) );
                    format = endptr;
                    continue;
                }
                case ' ': case 't': case 'n': ++format; continue;
                case 'B': pack_helper<uint8_t> (value); break;
                case 'H': pack_helper<uint16_t>(value); break;
                case 'I': pack_helper<uint32_t>(value); break;
                case 'L': pack_helper<uint64_t>(value); break;
                case 'b': pack_helper<int8_t>  (value); break;
                case 'h': pack_helper<int16_t> (value); break;
                case 'i': pack_helper<int32_t> (value); break;
                case 'l': pack_helper<int64_t> (value); break;
                case 'f': pack_helper<float>   (value); break;
                case 'd': pack_helper<double>  (value); break;
                case 'r': pack_raw             (value); break;
                case 'g': pack_generic         (value); break;
                case 'x': skip(); len=1; format++; continue;
                case '*': {
                    if constexpr (convertible_to<decltype(value), size_t>) {
                        len = static_cast<size_t>(value);
                        packf(++format, args...);
                        return;
                    } else {
                        throw logic_error("Invalid size type.");
                    }
                }
                default:
                    throw logic_error("Unexpected character (" + std::string{*format} + ") in format string.");
            }
            len = 1;
            packf(++format, args...);
            return;
        }
        throw logic_error("Too many arguments for format string.");
    }
};

} // end namespace pack

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.

Struct auxiliar em c

Olá, pessoal! Bom dia, tudo bem? Eu tenho uma atividade da faculdade ontem eu tenho que fazer uma lista em C onde cada posição da lista armazene 3 dados diferentes sobre um competidor! Como eu posso fazer para colocar as 3 informações diferentes em cada posição da lista? Eu nem comecei o código ainda

c – Error al usar struct con indice 0 y medicion de un struct dinamico

Estoy creando un codigo que calcula distancias entre puntos de un plano cartesiano, y deberia de sacar las distancias menores entre ellas, uso un struct dinamico con malloc para guardar los datos:

struct puntos
{
      int idPunto;
      float valorX;
      float valorY;
      float Modulo;
      float Angulo;

};

La funcion de las distancias es esta:

float distancia(float X1, float Y1,float X2,float Y2){
float resultado;

resultado= sqrt(((X2-X1)*(X2-X1)) + ((Y2-Y1)*(Y2-Y1)));

return resultado;

}

El codigo para encontrar las distancias menores es este:

    min = 1000000;
    puns = fopen("resultadosDeDistancias.txt","w+");
    unsigned int tamanio = sizeof(punto)/sizeof(*punto);

    for(i=0;i<tamanio;i++){
        for(j=0;j<tamanio;j++){
            if(j != i){
                dis = distancia(punto(i).valorX,punto(i).valorY, punto(j).valorX, punto(j).valorY);
                if(dis<min){
                    min = dis;
                    dis2=j+1;
                }
                else{
                    min = min;
                }
            }
        }
        fprintf(puns, "Para el punto %d, el punto %d es el que a menor distancia se encuentra: %.3f un",i+1,dis2,min);
        min = 1000000;
    }
    fclose(puns);
    char dato;

    arch=fopen("resultadosDeDistancias.txt","r");

    if(arch==NULL){
        printf("Error, no se encuentra el archivon");
    }

    else{
        while((dato = fgetc(arch)) != EOF){
            printf("%c",dato);
        }
        fclose(arch);
        printf("n");
    }

El problema es que al calcular el primer punto me aparece como (0,0) y por ejemplo al calcular 10 distancias solo calcula 8 con puntos que no existen en mi struct. No estoy seguro si es problema de logica o estoy colocando algun comando erroneamente.

estructuras de datos – Problema con Struct dinamicos C

Estoy haciendo un programa en C que consiste en crear puntos de 2 coordenadas (x, y) tanto de fora aleatoria como manualente. Luego almacenar unos datos de los mismos puntos en un struct. Despues almacenarlo en compendioDePuntos.txt.

El struct:

struct puntos{
 int idPunto;
 float valorX;
 float valorY;
 float Modulo;
 float Angulo;
}

Para asignar puntos manualmente:

scanf("%d", &aux1);
punto=(struct puntos*)malloc(sizeof(aux1));
if(punto == NULL){
    printf("Error en Malloc()");
}
else{
    arch = fopen("compendioDePuntos.txt","w+");
    if(arch==NULL){
        printf("No se pudo crear el archivo");
    }
    else{
        for (i = 0; i < aux1; i++){
                Valv:
        //    textcolor(RED+BLINK);
        //    cprintf("****Coloca unicamente valores entre -200 a 200****n");
                  punto(i).idPunto = i+1;
                  printf("Ingresa el Valor en x del punto numero %i n", i+1);
                  scanf("%f",&punto(i).valorX);
                  printf("Ingresa el Valor en y del punto numero %i n", i+1);
                  scanf("%f",&punto(i).valorY);

                if(punto(i).valorX>200 || punto(i).valorY>200 || punto(i).valorX<(-200) || punto(i).valorY<(-200) ){
            //cprintf("Error, porfavor solo pon valores de -200 a 200 unidades porfavor");
            goto Valv;
                }
            else{
                    punto(i).Modulo=modulo(punto(i).valorX,punto(i).valorY);
                    punto(i).Angulo=angulop(punto(i).valorX,punto(i).valorY);

                    fprintf(arch,"%d",punto(i).idPunto);
                    fprintf(arch,"t%.3ft" ,punto(i).valorX);
                    fprintf(arch,"t%.3ft" ,punto(i).valorY);
                    fprintf(arch,"t%.3ft" ,punto(i).Modulo);
                    fprintf(arch,"t%.3fn" ,punto(i).Angulo);
            }
        system("cls");
        }
    fclose(arch);
    }
free(punto);
imprimir();
}

y para asignar valores al azar uso la funcion numeror:

float numeror(int num){
 float ranint,ransig;

    num = num+1;

    ranint=rand() % num;
    ransig=rand() % 2;

    if(ransig=0){
        ranint = -ranint;
    }
    else{
        ranint = ranint;
    }

 return ranint;
}

El problema que tengo es que al intentar introducir mas de 3 puntos manualmente el archivo txt queda en blanco, si lo hago de forma manual, pero si lo hago de forma aleatoria, el archivo parece que se corrompe puesto que enseña muchos simbolos ASCII al azar. Creo que es por un error de memoria porque mi compilador (gcc) marca un Aborted (core dumped), aunque la verdad no se si sea eso.