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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:35:33+00:00 2026-06-10T23:35:33+00:00

Here is my code: #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; struct

  • 0

Here is my code:

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


using namespace std;

struct Computer
{
    char * model;
    char * assembler;
    int processorInt;
};

int main()
{
    Computer comp;
    char* model;
    char* assembler;
    int processorInt;

    cin>>model;
    cin>>assembler;
    cin>>processor int;

    comp.model = model;
    comp.assembler = assembler;
    comp.processorInt = processorInt;

    return 0;
}

//if i do so, it works, BUT if i do in another way, it gives Segmentation Fault

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


using namespace std;

struct Computer
{
    char * model;
    char * assembler;
    int processorInt;
};

void setValues()
{
    Computer comp;
    char* model;
    char* assembler;
    int processorInt;

    cin>>model;
    cin>>assembler;
    cin>>processor int;

    comp.model = model;
    comp.assembler = assembler;
    comp.processorInt = processorInt;
}

int main()
{
    setValues();

    return 0;
}

So what’s the reason?

My goal is to create an array of structures in which i can save some info about each “computer”, then make a possibility to edit any structure and then sort the whole array by procesorInt. But I can’t even create a normal editable structure.

  • 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-10T23:35:35+00:00Added an answer on June 10, 2026 at 11:35 pm

    Neither version works, you’re trying to read data into an uninitialized pointer. Just declaring e.g. char* model; leaves you with a pointer that isn’t initialized: it can point to any spot in memory. When you try to store a string at that location using cin >> you’ll be writing memory that doesn’t belong to you. This might segfault, or it might appear to work.

    Just declaring char* doesn’t give you space for the string: if you’re using standard C strings then you’ll need to give them some data: make your char pointers arrays of a fixed size, or use malloc to allocate a string buffer:

    char model[MAX_STRING_LENGTH];
    

    Or use malloc, but be aware that you’ll need to free this memory later:

    char *model = malloc(MAX_STRING_LENGTH);
    

    Now, if you’re going to use standard C strings and cin together, you shouldn’t use >> at all: there’s no way to limit the input size. Instead use cin.getline with MAX_STRING_LENGTH as the limit (see the example in the docs for details).

    But definitely prefer to use std::string instead: if you do so then you won’t need to deal with providing space for the string yourself, and your code won’t need to change much at all.

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

Sidebar

Related Questions

here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
here is my code for xor linked list implementation #include<iostream> using namespace std; struct
Here is the code: #include <iostream> #include <time.h> using namespace std; #define ARR_LENGTH 1000000
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
here is code which prints longest string duplicated M times #include <iostream> #include <stdlib.h>
Here is my code #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h>
i have wrote code for quicksort with linked list ,here is code #include<stdio.h> #include<iostream>
Here is some code I wrote (using GCC's __restrict__ extension to C++): #include <iostream>

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.