Thread: ltoa causing seg fault!

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30

    ltoa causing seg fault!

    Hi, i am trying to convert a long integer to a string in hex format, here is what i have at the moment:

    Code:
    Parameters *executeInstruction(Parameters *params, char memory[8192][9], long opr, long opc, char *line, int trace){
    
    char *s;
    
    //.....blah blah blah other boring irrelevant code here.....
    
    s = ltoa(params->a, s, 16); //SEG FAULTS HERE
    
    }
    Parameters is a struct i made, 'a' is a field in that struct of type long. Is there anything blindingly obvious i have missed here or is this going to be an annoying one?
    thanks!

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Are you sure that 'char *s' points to a valid memory location? Just declaring a function parameter an array won't actually allocate space for it.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Hmm, i've tried char s[10] instead but that gives me an incompatible types error at compile. Should i be doing calloc or malloc or something?

    Ah, getting rid of the "s = " part at the beginning of the line solves this. Woohoo, thanks again!
    Last edited by boblettoj99; 05-12-2010 at 06:15 AM.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    char memory[8192][9] is an array of arrays, not an array, I hope you know that.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Its meant to be an array of strings, so a char[][] right?
    Also i have another problem, under gcc in windows this compiles fine but under gcc in linux i get an error: undefined reference to 'ltoa'

    any ideas?

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Maybe because it is not a standard function and you have enabled ansi conformance? Or it is not in your library on Linux.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Hmm, is there a way to print to a string? like this?:
    Code:
    fprintf(myChar, "%.8lX", myLong);
    that would make the conversion process a lot easier..

    OH WAIT SPRINTF
    Last edited by boblettoj99; 05-12-2010 at 07:09 AM.

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    That's what snprintf is for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  4. lsass is causing a seg fault
    By skorman00 in forum Tech Board
    Replies: 3
    Last Post: 07-02-2004, 09:33 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM