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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:28:37+00:00 2026-06-15T10:28:37+00:00

I have the following code which simply prints out an introduction for a person’s

  • 0

I have the following code which simply prints out an introduction for a person’s name.

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

typedef struct {
    char* firstname;
    char* lastname;
}Person;

void intro(void *person){
    printf("The person you are looking for is %s %s\n", ((Person *)person)->firstname, ((Person *)person)->lastname);
}

int main()
{
    Person *a = NULL;
    a = (Person *)malloc(sizeof(Person));

    char *first = NULL, *last = NULL;
    first = (char *)malloc(sizeof(char)*20);
    strncpy(first,"Bob", 20);
    last = (char *)malloc(sizeof(char)*20);
    strncpy(last,"Newmonson", 20)
    a->firstname = first;
    a->lastname = last;

    intro(a);

    return 0;
}

Produces the output

The person you are looking for is Bob Newmonson

However changing intro(a) to intro(&a) produces

The person you are looking for is �@ Newmonson

When I open the first attempt in GDB and break on line 10 I find the address of person=0x601010. Both the first name and last name are stored where I would expect, 0x04006b9 and 0x4006bd since they where declared earlier in the stack.

What gets me is when I run GDB with the changes made to intro(&a). The address of person is now 0x7fffffffffdd38, with the first name pointing to 0x601010 and the last name pointing to 0x4006db.

Can anyone help explain to be what is going on and why I can still access the proper address of the last name in the second test.

EDIT :

As everyone seems to keep asking about it the void * was for a threading portion of this code that I did not include.

  • 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-15T10:28:37+00:00Added an answer on June 15, 2026 at 10:28 am

    It’s because a is already pointer to a Person structure; therefore intro(&a) passes a pointer to that pointer, but intro() treats it’s argument as a pointer to Person.

    Also, if intro() is intended to work on a Person, it should declare a Person * argument, not a void *.

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

Sidebar

Related Questions

We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
I have the following thread which simply prints a dot every 200ms: public class
I have the following code which creates a simple window with two buttons which
I have the following very simple code snippet which is loaded from a separate
I have the simple following code, which is working in a ruby (not rails)
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to

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.