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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:29:50+00:00 2026-05-23T12:29:50+00:00

int main() { char **k; char *s =abc; char *b =def; k = &s;

  • 0
int main() {


  char **k;
  char *s ="abc"; 
  char *b ="def";    

  k = &s;
  k++;
  k = &b;
  cout<<*(k - 1)<<endl; // nothing but newline. Shouldn't I get "abc"? 
  //EDIT: corrected a typo should be *(k - 1)  
}

I got nothing but a newline from cout. When I looked at the behavior of char* I get the impression that since I have the address of of the first character I could use char* like an array, which is true. However, for char** this behavior seems to be totally different, when I tried k++ it doesn’t seem to behave like an array. Why is that?

Also when I tried (K + 1) = &b; I got an error, why couldn’t I do that?

  • 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-23T12:29:50+00:00Added an answer on May 23, 2026 at 12:29 pm

    k is a pointer to a pointer to a char, so when you advance it, it does not walk down the string as you’re expecting it to. Instead, it now points to the char * that is located adjacent to the one it was pointing to. In your case there is none, so you’re de-referencing a random memory location when you print the value of *(k - 1).

    (K + 1) = &b;
    

    The above is an error because you must have an l-value on the left side of an assignment, it cannot be a temporary expression (r-value).

    EDIT:
    Here’s an example that’ll hopefully be easier to follow than reading about the mistake you’ve made.

    int main()
    {
      char **k;
      char *s[2] = {"abc", "qwerty"};
    
      k = &s[0];
      std::cout << *(k + 1) << std::endl; // prints "qwerty"
    
      k++;
      std::cout << *(k - 1) << std::endl; // prints "abc"
    
      std::cin.get();
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int main() { char **k; char *s =abc; char *b =def; *k = s;
int main() { char *name = new char[7]; name = Dolphin; cout << Your
int main() { char** k; k = new char*; char* k1 = abc; char*
The program is: #include <stdio.h> #include <stdlib.h> int main(void) { char *a=abc,*ptr; ptr=a; ptr++;
This is my code #include <stdio.h> void abc(char *text); int main(void) { char text[20];
Code : #include stdio.h #include string.h int main() { char *p = abc; printf(p
Here's my program so far: int main() { char choice = 'D'; string inputString;
Why does the following code seg fault before returning: int main() { char iD[20];
#include <cstring> int main() { char *pName = new char[10]; char dummy[] = dummy;
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())

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.