Solved

Problem with C pointers

Posted on 2003-10-23
5
201 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now