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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:47:52+00:00 2026-06-01T05:47:52+00:00

Possible Duplicate: Problem with processing individual strings stored in an array of pointers to

  • 0

Possible Duplicate:
Problem with processing individual strings stored in an array of pointers to multiple strings in C

Ok so I’m trying to change a char of a string to another char in C. The thing is, each string is an element of a 1D array so essentially all together its a 2D array because a string itself is an array of chars. Anyways I have a problem creating code to do this. Is it even possible to do this? Any help is appreciated.

Here’s the code:

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

int main ()
{ 
int i, size;
char **a;

a=(char**)malloc(sizeof(char*));

printf("Enter the size of the array:");
scanf("%d", &size);

for(i=0;i<size;i++){
 a[i]=(char*)malloc(sizeof(char)*8);
}

a[3]="Read";


while(*(a[3])!='\0'){
 if(*(a[3]) == 'e'){
    *(a[3]) = 'r';
 }
}

 printf("%s\n", a[3]);

 system("pause");
 return 0;

}
  • 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-01T05:47:53+00:00Added an answer on June 1, 2026 at 5:47 am

    You didn’t allocate enough space for a. Instead of

    a=(char**)malloc(sizeof(char*));
    

    you need

    a=(char**)malloc(sizeof(char*)*size);
    

    and obviously this must move to be after size has been read.

    Once you sort that rather mundane problem out the fundamental problem is here:

    a[3]="Read";
    

    This makes the pointer a[3] point at a literal which cannot be modified. Instead you need to copy the contents of that literal into a[3]. Like this:

    strcpy(a[3], "Read");
    

    You must understand that a[3]=... assigns just the pointer a[3] and does not modify the string to which a[3] points.

    Now, your code will obviously be in error if size is less than 4 since then a[3] would be out of bounds, but I guess a[3] is just transient while you debug this.

    Your while loop is all wrong. Judging from your comments you want something like this:

    char *p = a[3];
    while (*p != '\0')
    {
        if (*p == 'e')
            *p = 'r';
        p++;
    }
    

    No need to cast the return value of malloc in C, so remove the casts. sizeof(char) is always equal to 1 so you can remove that too.

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

Sidebar

Related Questions

Possible Duplicate: What is the exact problem with multiple inheritance? Why is multiple inheritance
Possible Duplicate: C++ std::string conversion problem on Windows How to convert std::string to LPCSTR?
Possible Duplicate: Raphaeljs and Internet Explorer, problem when clicking an element I'm trying to
Possible Duplicate: Android Launch an application from another application I am having a problem
Possible Duplicate: Android AdMobs problem. I am trying to put in an ad but
Possible Duplicate: Intitialzing an array in a C++ class and modifiable lvalue problem As
Possible Duplicate: Parse C# string to DateTime I am facing a problem while converting
Possible Duplicate: c++ template problem Hi Everyone: I'm stuck trying to do the following:
Possible Duplicate: java operator ++ problem public class A { public static void main(String[]
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc

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.