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

The Archive Base Latest Questions

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

I’m trying to pass a char* and change it in another function but somehow

  • 0

I’m trying to pass a char* and change it in another function but somehow it keeps giving seg fault.

Full code is:

#include <stdio.h>

void getString(char** str) {
    *str[0] = '$';
    char c;
    int i = 1;
    while ((c = getchar()) != '$') {
        if (c != '\n') {
            *str[i-1] = c;
            i++;
        }
    }
    *str[i] = '\0';
}

int main (int argc, char *argv[]) {

    char* str =  (char*)malloc(200 * sizeof(char));
    while (1) {
        getString(&str);
        printf("String: %s\n",str);
    }
    return 0;
}

If I take the * from str[0] = ‘$’ it gives a warning passing pointer to integer.

Dunno where I’m messing up.

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

    the array index , [], operator has lower precedence than the dereference operator, *

    So you’d have to change your code to do

     (*str)[0] = '$';
     (*str)[i-1] = c;
     (*str)[i] = '\0';
    

    In your case, you don’t need to pass in the address of your pointer, just pass the pointer:

    getString(str);
    

    And access it as

    str[0] = '$';  
    str[i-1] = c;
    str[i] = '\0';
    

    Since you’re using malloc(), be sure to add #include <stdlib.h>
    If you didn’t cast the return value of malloc, which is not needed in C, you should have
    gotten a warning regarding this.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.