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

  • Home
  • SEARCH
  • 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 4040346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:44:30+00:00 2026-05-20T12:44:30+00:00

I’m trying to better understand c, and I’m having a hard time understanding where

  • 0

I’m trying to better understand c, and I’m having a hard time understanding where I use the * and & characters. And just struct’s in general. Here’s a bit of code:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int *ptr;
    *ptr = &R;
    &ptr[0] = 1;
    printf("this is R at spot 0: %d", ptr[0]);
}  

lc3_word_t is a struct defined like this:

struct lc3_word_t__ {
  BIT b15;
  BIT b14;
  BIT b13;
  BIT b12;
  BIT b11;
  BIT b10;
  BIT b9;
  BIT b8;
  BIT b7;
  BIT b6;
  BIT b5;
  BIT b4;
  BIT b3;
  BIT b2;
  BIT b1;
  BIT b0;
};

This code doesn’t do anything, it compiles but once I run it I get a “Segmentation fault” error. I’m just trying to understand how to read and write to a struct and using pointers. Thanks 🙂


New Code:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int* ptr;
    ptr = &R;
    ptr->b0 = 1;
    printf("this is: %d", ptr->b0);
}
  • 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-05-20T12:44:30+00:00Added an answer on May 20, 2026 at 12:44 pm

    Here’s a quick rundown of pointers (as I use them, at least):

    int i;
    int* p; //I declare pointers with the asterisk next to the type, not the name;
            //it's not conventional, but int* seems like the full data type to me.
    
    i = 17; //i now holds the value 17 (obviously)
    p = &i; //p now holds the address of i (&x gives you the address of x)
    *p = 3; //the thing pointed to by p (in our case, i) now holds the value 3
            //the *x operator is sort of the reverse of the &x operator
    printf("%i\n", i); //this will print 3, cause we changed the value of i (via *p)
    

    And paired with structs:

    typedef struct
    {
        unsigned char a;
        unsigned char r;
        unsigned char g;
        unsigned char b;
    } Color;
    
    Color c;
    Color* p;
    
    p = &c;     //just like the last code
    p->g = 255; //set the 'g' member of the struct to 255
                //this works because the compiler knows that Color* p points to a Color
                //note that we don't use p[x] to get at the members - that's for arrays
    

    And finally, with arrays:

    int a[] = {1, 2, 7, 4};
    int* p;
    
    p = a;    //note the lack of the & (address of) operator
              //we don't need it, as arrays behave like pointers internally
              //alternatively, "p = &a[0];" would have given the same result
    
    p[2] = 3; //set that seven back to what it should be
              //note the lack of the * (dereference) operator
              //we don't need it, as the [] operator dereferences for us
              //alternatively, we could have used "*(p+2) = 3;"
    

    Hope this clears some things up – and don’t hesitate to ask for more details if there’s anything I’ve left out. Cheers!

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

Sidebar

Related Questions

No related questions found

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.