Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:47:41+00:00 2026-05-16T06:47:41+00:00

I’m currently self-studying C for mastering an university project. In that project we have

  • 0

I’m currently self-studying C for mastering an university project. In that project we have several signatures given that we need to implement for solving our task. After several hours of trying to make some progress, I must admit that I’m totally confused about the return types of functions. I will show you some code:

This is a structure given to represent numbers in the power to the basis 32.

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

typedef unsigned int uint32;
typedef struct 
{
  int n;
  uint32* words;
}
foo;

We need to implement some functions like the one with the following signature:

foo add(foo* a, foo* b);

As can you see, the function takes two pointers to the structure as parameters, and returns the value of a structure, and not the pointer to it. That means that I have to locally create an instance of foo and return it.

foo add(foo* a, foo* b)
{
  foo result = {1, malloc(sizeof(uint32))};
  // do something with result
  return result;
}

1) Is there anything wrong in this attempt? In my production code I get problems with accessing the value of the returned structure: it seems to they have changed after the structure was returned. Unfortunately I was not able to reproduce that behaviour in a more simple application, which got me even more worried that I’m doing this all wrong. I have also tried to allocate memory with malloc for the variable result, but that only seems to work if I create a pointer to foo, which seems to be no option, because I can’t return the pointer.

2)
I’m looking for a way to cast (probably not the correct term) the return type back into a pointer.
Lets just say I have this code:

   foo* bar1 = malloc(sizeof(foo));
   bar1->n = 1;
   bar1->words = malloc(bar1->n * sizeof(uint32));
   bar1->words[0] = 4294967295;
   foo* bar2 = malloc(sizeof(foo));
   bar2->n = 1;
   bar2->words = malloc(bar2->n * sizeof(uint32));
   bar2->words[0] = 21;
   foo bar3 = add(bar1, bar2);

Works fine. How could I now use the value of bar3 to call the add function again. Like this:

   foo bar4 = add(bar1, bar3);

After also tried various attempts with creating foo* and trying to assign values, but they all failed and I decided to post this question to get some nice answer that helps me understanding C a little bit more.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:47:41+00:00Added an answer on May 16, 2026 at 6:47 am

    Update I withdraw my previous explanation. I ran your code through valgrind and indeed you don’t produce any leaks or incorrect memory accesses. The way you have it, the struct is returned by value, so it is copied into a new struct on return. The only thing you would have to do is free() the words pointer when you’re done.

    Your strange results might be the result of something you’re doing that’s not shown here.

    You need to return a pointer to foo:

    foo* add (foo* a, foo* n) {
      foo result* = malloc(...);
    
      /* do some addition */
    
      return result;
    }
    

    Don’t forget to free() the foo structs which are obtained this way.

    To undesrtand the reason why your way doesn’t work, consider the lifetime of variables declared within the add() scope:

    1. add() is entered.
    2. A new foo struct is created and assigned to the variable result.
    3. The function add() returns and the memory for variables within that scope is freed.
    4. Outside of add(), the caller receives a struct stored at a location in memory which is marked as unused.
    5. Attempts to access that struct produce undefined results because the memory where it is located could be allocated for other data.

    Consider the flow of the solution I presented:

    1. add() is entered.
    2. New memory is allocated on the heap and the address of that memory is stored in result.
    3. The function add() returns the value of result (an integer indicating an address in memory).
    4. Outside of add(), the location pointed at by result is still an allocated location on the heap, and the pointer returned may be safely dereferenced.
    5. When you are done with the struct at that location, you call free(result) and that location is marked as unused.

    Hope that helps! For further information, you should investigate what the memory heap is and how allocation works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.