You are here:

C/c pointer problem

Advertisement


Question
My reverse string program is crashing on "*str(str+i)=*(str+j);" line in the loop

char* strReverse(char* str)
{
  
  int length  = strlen(str);
  int i = 0;
  int j= 0;
  char c = '-';

  for ( i = 0,j =(length-1) ; i<j ; i++,j-- )
  {
     printf("checking # %s  .
", str);
     c= *(str+i);
     *str(str+i)=*(str+j);
     *(str+j)=c;

     
  }
  str[length]='

Answer
Hi Patrali

I have modified your code a bit please check out and in case of any doubt please feel free to ask me

#include<stdio.h>
#include<conio.h>
char* strReverse(char* str)
{

int length  = strlen(str);
int i = 0;
int j= 0;
char c = '-';

for ( i = 0,j =(length-1) ; i<j ; i++,j-- )
{
printf("checking # %s . ", str);
c= *(str+i);
*(str+i)=*(str+j);
*(str+j)=c;
}
return str;
}

void main()
{
  printf("Rev: %s",strReverse("http://www.scodz.com"));
}

I have a website for the C/C++ lovers if you want you can join me there also at http://www.scodz.com

regards
Joydeep

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.