Solved

Problem with C pointers

Posted on 2003-10-23
5
230 Views
Last Modified: 2010-04-15
I have a little pointer problem with the attached source code whereas the part staring with - "%02x", *(Family..... - is correct. The output in HtmlPage is unfortunately not correct, i.e. too long and weird so I guess I handled the pointers not properly. The given output contains admittedly the correct output as well at the end of the string.
Can somebody help me?
Best regards,
Mr Bone

char * getSerialNo(SMALLINT deviceNum){
      int x;
      char * temp;
      char * temp1="";
      for(x=7; x>=0; x--){
      sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
      strcat(temp1,temp);
      }
      return temp1;      
}

sprintf(HtmlPage, getSerialNo(deviceNum));
0
Comment
Question by:LarsBone
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605320
you are using sprintf and strcat on memory that you have neither allocated nor intialized ... from your code, it seems all you want is concatenated FamilySN[]

#define MAX_STRING_LENGTH 256

char * getSerialNo(SMALLINT deviceNum){
    int x;
    char * temp;
    char * temp1;
    temp = (char * ) malloc ( MAX_STRING_LENGTH );
    temp1=temp;
    for(x=7; x>=0; x--){
    sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
    temp = temp + strlen(temp);
    }
    return temp1;    
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9605322
if you are looking for members of the array to be concatenated
>sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
should be
sprintf(temp,"%02X", *(FamilySN[deviceNum + x]));
0
 

Author Comment

by:LarsBone
ID: 9605442
Unfortunately that did not work as well. The following code works fine but since I need it very often I want to create a function that returns the string (SerialNo), which is below pasted in the HtmlPage buffer.

for(iii=7; iii>=0; iii--)
{
  sprintf(tmpbuffer,"%02X", *(FamilySN[i]+iii));
  strcat(HtmlPage,tmpbuffer);
}
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9605451
sprintf(HtmlPage, getSerialNo(deviceNum));
should be

sprintf(HtmlPage, "%s",getSerialNo(deviceNum));
0
 

Author Comment

by:LarsBone
ID: 9605502
Right yeah. And after changing it to as follows it worked fine. THANKS!

char * getSerialNo(SMALLINT deviceNum){
    int x;
    char * temp;
    //char * temp1;
    temp = (char * ) malloc (16);
    //temp1=temp;
    for(x=7; x>=0; x--){
    sprintf(temp,"%02X", *(FamilySN[deviceNum]+x));
    temp = temp + strlen(temp);
    }
    return (temp-16);    
}
...
sprintf(tmpbuffer,"%s",getSerialNo(i));
strcat(HtmlPage,tmpbuffer);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question