Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with C pointers

Posted on 2003-10-23
5
Medium Priority
?
256 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

824 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