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 8626627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:04:46+00:00 2026-06-12T08:04:46+00:00

I have a structure that represents a row in a table: typedef struct {

  • 0

I have a structure that represents a row in a table:

typedef struct {
    char *a;
    char *b;
} row;

and I have a function that initializes that row based on db data and returns a pointer to it

row* get_row(dbrow d) {
    row *r = malloc(sizeof(row));
    r->a = malloc(5);
    strcpy(r->a, d.a);
    r->b = malloc(5);
    strcpy(r->b, d.b);
    return r;
}

and finally, I have a function that has an row **rows as a parameter:

void get_rows(row **rows) {
    ...
    rows = malloc(rowNumber * sizeof(row*));
    int i;
    for (i = 0; i < rowNumber; i++) {
        rows[i] = get_row(dbrow);
    }
}

get_row works as expected and returns a pointer to valid row struct, but gdb shows that rows[0] (and all the others) never gets a new value, that is, it always points to the same address, almost as if the rows[i] = get_row(dbrow) line doesn’t exist.

  • 1 1 Answer
  • 0 Views
  • 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-06-12T08:04:47+00:00Added an answer on June 12, 2026 at 8:04 am

    …gdb shows that rows[0] (and all the others) never gets a new value…

    I’m assuming here that you are looking at the return value of your get_rows function, not at the value of its local variable rows. Here is the problem:

    rows = malloc(rowNumber * sizeof(row*));
    

    You are assigning a new value to a copy of the original pointer that the function received, not the original. This change will not be visible outside the function.

    If you need to assign a new value to the argument then you will need to add another level of indirection. Remember; everything in C is passed by value. So, your function should take a row*** as its argument:

    void get_rows(row ***rows) {
        if(!rows) {
            signal_some_error();
            return;
        }
        ...
        *rows = malloc(rowNumber * sizeof(row*));
        ...
    }
    

    Also, as user1700513 pointed out, you are assigning a row* to a row. That can’t be your actual code as it would result in a compiler error.

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

Sidebar

Related Questions

I have a structure called Patch that represents a 2D array of data. newtype
I have a data structure that represents a directed graph and I'm looking for
I have set up a circular linked list data structure that represents a word,
I have a data structure that represents C# code like this: class Namespace: string
Given a table that represents a hierarchical tree structure and has three columns ID
Let's say that I want a class that represents a data structure in memory.
I have a large data structure that is using striping to reduce lock contention.
Some background I have a set of data that represents the alchemy ingredients and
I have a button that will create a new entry (row in a table)
I have a table of quarterback statistics. Each row in the table represents one

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.