The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #9 (permalink)  
Old 11-14-2007
n1djs n1djs is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 12
Smile Wierd C program. Help Needed

You have a problem in both cases. One is being caught, the other is not.
"char *i, j[15], *k" says the following:
Point to memory and call that pointer i;
Create space in memory to hold 15 characters and call that space j;
and point to memory and call that pointer k;

When you try to do "sprintf( k, "print.sh %s", i );" you are asking the computer to stick the string "print.sh ?" in the memory location you pointed to with the pointer variable k. Your problem is that you have not allocated any space to hold the string at the pointer location of k, k is currently just pointing to some random memory location and no space has been allocated to hold anything at the memory location pointed to by k.

That is the reason it dies. The reason it doensn't die (yet) in the example where j is allocated space, is that there is at least some space allocated on the stack, and k is probably accidently pointing there, and therefor corrupting j, but not causing a segv (yet).

I suggest you go study up on the differences on *k, k[10], k[10,10] and **k. When you understand what these different forms mean, you're sysadmin will have a much better day.