Solved

Problem with C pointers

Posted on 2003-10-23
5
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 recursion in the C programming language.

623 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