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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:18+00:00 2026-05-26T14:04:18+00:00

I am trying to deliver a char pointer to a function which then fills

  • 0

I am trying to deliver a char pointer to a function which then fills it with stuff so that the calling function could use it then. As it always gave me weird stuff in the calling function i wrote a simple representation about what I have done:

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

void bla(char *t)
{
    t = (char*) malloc(5);
    if (t != 0) {
        t[0] = 'h';
        printf("%c\n", t[0]);
    }
}

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

    bla(b);
    printf("%c\n", b[0]);
    return 1;
}

I’m not quite sure if it has something to do that C passes a copy of the argument. Do I need to pass a pointer to a pointer then or is there a better solution?

EDIT:

Sorry guys but I didn’t get it. Could you please look over this example:

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

void blub(char **t)
{
    printf("%d\n", *t);
    *t = (char*) malloc(sizeof(char) * 5);
    *t[0] = 'a';
    *t[1] = 'b';
    printf("%d\n", *t);
    printf("%d\n", *t[0]);
    printf("%d\n", *t[1]);
}

int main(int argc, char **argv)
{
    char *a;
    blub(&a);
    printf("%d\n", a);
    printf("%d\n", a[0]);
    printf("%d\n", a[1]);

    return 1;
}

Output is as follows:

./main
6154128
140488712
97
98
140488712
97
0      <== THIS SHOULD BE 98 AS ABOVE!?

Why do i get 98 in the function blafu and in main it is a null pointer?! I am totaly confused :/

  • 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-26T14:04:18+00:00Added an answer on May 26, 2026 at 2:04 pm

    You need to use pointer to pointer.

    //............vv
    void bla(char **t)
    

    Then use the pointer by dereferencing it:

    // note the '*' in front of t
    *t = (char*) malloc(5);
    if (*t != 0) {
        *t[0] = 'h';
        printf("%c\n", *t[0]);
    }
    

    Also, declare b as char*, not as char b[10].


    Why? Because you’re trying to change the pointer. The logic is the same as other types, but it’s kinda confusing here.
    Think about it like this: if you need to pass an int and you need to change it, you need a pointer to int. The same is here – you need to pass a pointer to char and you need yo change it, so use “pointer to pointer to char” 🙂


    EDIT:
    According to your edit – yes, you have understood this perfectly, there’s just a small problem – the priority of operator* and operator[]. If you replace your *t[X] with (*t)[X], everythng will be fine 🙂

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

Sidebar

Related Questions

I'm trying to develop an application server that will deliver content to a core
I'm trying to implement an ActionMailer function that will send out a newsletter to
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to perform a single boolean NOT operation, it appears that under MS SQL
I am trying to understand conceptually the best way to deliver real streaming audio
I'm trying to use jQuery EasySlider to carousel, however, when I specify a number
I am trying to deliver an SSIS package to SQL Server 2005 Enterprise Edition
If I have significant amounts of text and am trying to discover templates that

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.