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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:17:46+00:00 2026-06-03T06:17:46+00:00

This is my code: #include <stdio.h> #include <stdlib.h> void getinfo(unsigned int a, unsigned int

  • 0

This is my code:

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

void getinfo(unsigned int a, unsigned int b, char **s);

int main(){
    unsigned int len_max = 8;
    unsigned int current_size = 0;
    char *pStr = malloc(len_max);
    if(pStr == NULL){
        perror("\nMemory allocation\n");
        return EXIT_FAILURE;
    }
    current_size = len_max;

    printf("Inserisci hostname: ");
    getinfo(len_max, current_size, &pStr);
    printf("\nLa stringa inserita è: %s\n", pStr);
    free(pStr);
    return EXIT_SUCCESS;
}

void getinfo(unsigned int a, unsigned int b, char **pStr){
    unsigned int i = 0;
    char c = EOF;
    while((c = getchar()) != '\n'){
        *pStr[i++] = (char)c;
        if(i == b){
            b = i+a;
            if((*pStr = realloc(*pStr, b)) == NULL){
                perror("\nMemory allocation error\n");
                exit(EXIT_FAILURE);
            }
        }
    }
    *pStr[i]='\0';
}

When i execute this code i got a segmentation fault when i press enter (after i’ve wrote the string).
I’m sure the problem is into the function (probably the problem is the *s pointer) but i don’t know how to correct it…

  • 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-03T06:17:48+00:00Added an answer on June 3, 2026 at 6:17 am

    You have a precedence problem. You need to use

    (*s)[i++] = ...
    

    instead of

    *s[i++] = ...
    

    Similarly you need

    (*s)[i]='\0';
    

    When you write *s[i] you are indexing s. But you want to index *s, and hence require the parentheses.

    I’ve not checked the rest of your code, but I hope this helps you on your way to debugging the rest of it, if indeed there are more errors.

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

Sidebar

Related Questions

This is my code: #include <stdio.h> #include <stdlib.h> #include <string.h> void getinfo(unsigned int a,
This is the code: #include <stdio.h> #include <stdlib.h> void acceptMaze(char maze[ROW][COLOUMN], int nrows, int
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> void getData(short int *number, char *string)
I have this very simple code: #include <stdio.h> #include <stdlib.h> int maxArr(int *arr) {
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
Is this code standard conforming ? #include <stdio.h> #include <cstdio> int main() { FILE
I'm facing segmentation fault problem when trying to run this code: #include<stdio.h> #include<stdlib.h> int
The code: #include stdafx.h #include stdio.h #include math.h #include stdlib.h #include time.h int main()

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.