1

I'm trying to build a program that modifies the element of an array through a pointer to a pointer. I loaded it into the debugger and I see that the value of my pointer changes, but for some reason that doesn't affect the element in the array. Is my pointer syntax wrong? Am I reassigning my pointer somewhere else?

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

#define SIZE 6

/*
 * 
 */


void change (char **x);

int main() {
    
    char arra[] = "Back";
    char *c = arra;
    
    change(&c);
    
    int i; 
    printf("%c", arra[0]);
    }
    
   

void change (char **x) {
    
    *x = "H";
    
}

0

2 Answers 2

3
*x = "H";

should be

**x = 'H';

You are trying to modify the first character and character has to be within single quotes.

There is no need of pointer to pointer here. You can just pass the array which decays to a pointer when passed in the function parameterks as shown by @haccks

Sign up to request clarification or add additional context in comments.

1 Comment

Does and from your statement mean that's why?
2

No need to use pointer to pointer in this case. Just use pointer to char.

void change (char *x);  

and call it as

change(c); 

with the function body

 void change (char *x) { 
    *x = 'H';   
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.