logo
Tags down

shadow

Invalid number comes when accessing member structures


By : Ramakrishnan Kalyana
Date : October 14 2020, 02:22 PM
seems to work fine As suggested by Klaus Gütter use call by reference, to make changes to original object.
code :
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct DailyCost
{
    int dd;

} Day;
FILE *fp;
void add_dailycost();
void display();
void mainmenue();
int main()
{

    int j;
    fp = fopen("/home/mohit/projects/c/gedit_Dailyost/TextFiles/fileb.txt", "w+");
    rewind(fp);
    mainmenue();
    return 0;
}
void mainmenue()
{
    struct DailyCost day1;
    int j;
    printf("Enter 1 to add dailycost and enter 2 display dailycost.");
    scanf("%d", &j);
    switch (j)
    {
    case 1:
        add_dailycost(&day1);
        break;
    case 2:
        display(&day1);
        break;
    }
}
void add_dailycost(struct DailyCost* Day) /*if you pass simply 
  struct DailyCost , all the contents of original objects will be copied 
  into a new memory area, to be used in this function and will be 
  destroyed when this function exits. */
/* if you pass just the address of your object,you are working on 
   the same memory area as you passed from,in this case from mainmenue. */
{
    if (fp != NULL)
    {
        fseek(fp, 0L, SEEK_END);
    }
    int j;
    printf("Enter Day = ");
    scanf("%d", &Day->dd);
    fwrite(Day, sizeof(struct DailyCost), 1, fp);
    printf("If menmenue press 1 and exit press 0 = ");
    scanf("%d", &j);
    if (j == 1)
    {
        mainmenue();
    }
    else
    {
        printf("\nThank you");
        fclose(fp);
        exit(0);
    }
}
void display(struct DailyCost* Day) /* passing the object instead 
  of reference would also work here. But just for the display 
  purpose, you need not to unnecessarily create a new memory. */
{
    fread(Day, sizeof(struct DailyCost), 1, fp);
    rewind(fp);
    printf("Day =   %d\n", Day->dd);
    fclose(fp);
}


Share : facebook icon twitter icon

Accessing static member through invalid pointer: guaranteed to "work"?


By : sanupma
Date : March 29 2020, 07:55 AM
wish help you to fix your issue
it's not clear whether "evaluation" here involves an actual dereference.

Invalid read of size 1 when accessing a member of a struct


By : Khag
Date : March 29 2020, 07:55 AM
To fix this issue I've been running into this same issue for a few days now and have managed some workarounds but I need to understand where I am going wrong. , On the line:
code :
fprintf(stdout, "Name is %s", 40, data.ticket_name);

Accessing member of parent class "Invalid use of non-static data member" C++


By : user3433249
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to implement a child class that inherits the constructor from a parent class and subsequently access a member of the parent class which is initialized in that constructor.
code :
void myFunc()
{
    myClass tmp = foo;
}
void child::myFunc()
{
    myClass tmp = foo;
}

Valgrind complains of invalid read when accessing struct member through pointer to struct


By : goldencleric
Date : March 29 2020, 07:55 AM
around this issue As mentioned in the comments by @BoPersson, you add a local variable to the vector:
code :
std::vector<section_*> get_sections(){
    std::vector<section_*> sections;
    section_ sec1;    // <- Local, temporary variable 
    sec1.start = 0;
    ...
    sections.push_back(&sec1); // <- Address of local var
    return sections;
    // When this function ends, sec1 is no longer valid
}

Accessing a member of a structure in an array of structures which is a member of an array of different structures


By : hui kang
Date : March 29 2020, 07:55 AM
wish helps you I have a structure called Object defined as: , You can't use simple assignment with strings. You need to use strcpy.
code :
strcpy(rooms[0][0].objects[0].name, "Cabinet");
Related Posts Related Posts :
  • UARTs & Registers
  • trying to understand recursion in C
  • Why in a bidimensional static array arr, arr is the same thing as *arr
  • How can we produce the number x.y, where x and y are the two arguments or a function?
  • What does printf return c but getting an error
  • C, dividing a string in half
  • Waiting on multiple semaphores or multiple message queues
  • How can resort a string array in C using pointers only
  • warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  • Passing typedef function pointer as an argument problem
  • Checking 5 ints in an array if 3 are the same
  • Conflicting types error and pointer from integer without cast warning C
  • Simple IF Else, but crashes the Command Box when I try to generate an error
  • MACRO execution steps in C
  • How to save pointer in JavaScript and retrieve further
  • Value of local static variable changes between function calls
  • C program forcing me to have return type but I don't need it.!
  • fgets reads newline character from prompt
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org