Segmentation fault

S

somenath

Hi All,
Can any body explain why the bellow mention crashes wwhen I try to
execute.

#include<stdio.h>
#include<stdlib.h>
void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP);

int main(void )
{
char * s ="34567";
int x = 5;
int *iptrP=NULL;
convert_cpp_string_to_int(s,iptrP);
printf("INTEGER = =%d \n", *iptrP);
return 0;
}
void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
{
*integer_valueP = atoi( cpp_string);

}

But If i modify my code as mention bellow it does not crash
#include<stdio.h>
#include<stdlib.h>
void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP);

int main(void )
{
char * s ="34567";
int x = 5;

int *iptrP=&x;
convert_cpp_string_to_int(s,iptrP);
printf("INTEGER = =%d \n", *iptrP);
return 0;
}
void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
{
*integer_valueP = atoi( cpp_string);

}

Regards,
Somenath
 
C

Christopher Benson-Manica

somenath said:
int main(void )
{
char * s ="34567";
int x = 5;
int *iptrP=NULL;

You've explicitly said "This pointer doesn't point anywhere", so
naturally bad things are bound to happen when you dereference it. If
you make it point somewhere useful, say for example to enough memory
to store an int,

int *iptrP;
/* ... */
iptrP=malloc( sizeof *iptrP );

, all will be well.

Note that in the second program you posted,
int x = 5;
int *iptrP=&x;

you've said "The pointer points at x", which is fine.
 
S

somenath

You've explicitly said "This pointer doesn't point anywhere", so
naturally bad things are bound to happen when you dereference it. If
you make it point somewhere useful, say for example to enough memory
to store an int,

int *iptrP;
/* ... */
iptrP=malloc( sizeof *iptrP );

, all will be well.

Note that in the second program you posted,


you've said "The pointer points at x", which is fine.

If I change the code as following will it cause any problem ?
#include<stdio.h>
#include<stdlib.h>
void convert_cpp_string_to_int(char * cpp_string,int
*integer_valueP);


int main(void )
{
char * s ="34567";
int x = 5;
int iptrP;
convert_cpp_string_to_int(s,&iptrP);
printf("INTEGER = =%d \n", iptrP);
return 0;


}


void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
{
*integer_valueP = atoi( cpp_string);


}
 
S

santosh

Please remove sig blocks, like the above, unless you're commenting on
them.
If I change the code as following will it cause any problem ?
#include<stdio.h>
#include<stdlib.h>
void convert_cpp_string_to_int(char * cpp_string,int
*integer_valueP);

Your names are exceedingly verbose, spoiling readability.
int main(void )
{
char * s ="34567";
int x = 5;
int iptrP;
convert_cpp_string_to_int(s,&iptrP);
printf("INTEGER = =%d \n", iptrP);
return 0;
}

void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
{
*integer_valueP = atoi( cpp_string);
}

Why do you want to encapsulate a trivial function like atoi? Atleast
with strtoXX, I can understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top