Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why this dies
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Thu Jan 19, 2006 9:16 pm   Post subject: Why this dies

code:
#include <stdio.h>
#define PATH "/usr/tmp"

char *full_name(const char name[]);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system ("PAUSE");
  return (0);
}

char *full_name(const char name[])
{
  static char file_name[100];
  strcpy(file_name, PATH);
  strcat(file_name, '/');
  strcat(file_name, name);
  return(file_name);
}


Why does this program die a horribly gruesome death?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 19, 2006 9:29 pm   Post subject: (No subject)

code:
#include <stdio.h>
#define PATH "/usr/tmp"

char *full_name(const char* name);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system("PAUSE");
  return 0;
}

char *full_name(const char* name)
{
  char* file_name = malloc(sizeof(char) * 100);
  strcpy(file_name, PATH);
  strcat(file_name, "/");
  strcat(file_name, name);
  return file_name;
}


What did I do differently? Smile
Tubs




PostPosted: Thu Jan 19, 2006 9:46 pm   Post subject: (No subject)

Changed the parameter to a pointer and dynamically allocated memory for the variable?
Tubs




PostPosted: Thu Jan 19, 2006 9:47 pm   Post subject: (No subject)

that code is giving me errors!!! line 19 makes a pointer from integer without a cast
wtd




PostPosted: Thu Jan 19, 2006 9:55 pm   Post subject: (No subject)

Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC. Show me the exact code you were using.
Tubs




PostPosted: Thu Jan 19, 2006 9:58 pm   Post subject: (No subject)

wtd wrote:
Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC. Show me the exact code you were using.


The one you posted.
wtd




PostPosted: Thu Jan 19, 2006 10:04 pm   Post subject: (No subject)

Then I cannot help. The code I posted compiles flawlessly with GCC 3.4.2 (MinGW).
Tubs




PostPosted: Fri Jan 20, 2006 1:01 am   Post subject: (No subject)

Ok, I got the code working. IDEs are foolish.

Another question: how would I go about making a function that finds a certain item in a linked list, given the number it is in order and a pointer to the beginning of the list? This is what I have so far:

code:
#include <stdio.h>
#include "coaster_info_t.h"

coaster_info_t find_nth_coaster (int *N, int index)
{

  coaster_node_t *current = *N;
  int i = 0;

  if (current != NULL && i != index)
    current = current->next;

  else if (current != NULL && i == index)
    return( current );

  else
    return ( NULL );
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jan 20, 2006 1:13 am   Post subject: (No subject)

Use a loop to follow the links?
Tubs




PostPosted: Tue Jan 24, 2006 7:29 pm   Post subject: (No subject)

Yeah, thanks. New question if I may:

A sequence is defined by the following recursive relationship:

A(n) = A(n/2) + A(n/2) ; A(1) = 3

It is defined only for powers of 2 and you need only consider powers of two.

I don't think I know what they are asking here, this is my code so far:

code:
#include <stdio.h>

int A (int n);

int main(int argc, char *argv[])
{
  int n, x;

  printf ("n = ");
  scanf ("%d", &n);
  printf ("\n");

  x = A (n);
  printf ("A (n) = %.2f\n", x);
  system ("pause");

  return 0;
}

double A (double n)
{
  if (n == 1)
    return n;
  else
    n = A (n / 2) + A (n / 2);
}


Help greatly appreciated Smile
wtd




PostPosted: Tue Jan 24, 2006 7:34 pm   Post subject: (No subject)

Look at that code really closely.

Now... remove all of the code inside the functions, leaving just the headers? See the problem?
Tubs




PostPosted: Tue Jan 24, 2006 8:03 pm   Post subject: (No subject)

wtd wrote:
Look at that code really closely.

Now... remove all of the code inside the functions, leaving just the headers? See the problem?


Yeah, I was fooling around with the types because the question seemed to imply that that ints were ok, but there is division by 2 thrown in there. When I change it to double type it outputs 0, when it is an integer, 1. I'm still lost. Crying or Very sad
wtd




PostPosted: Tue Jan 24, 2006 9:58 pm   Post subject: (No subject)

What they're saying is... when n is 1, A returns 3. For any other value of n, well... the question explains that. So, let's look at a breakdown of A(8).

code:
A(8)
A(4) + A(4)
(A(2) + A(2)) + (A(2) + A(2))
((A(1) + A(1)) + (A(1) + A(1))) + ((A(1) + A(1)) + (A(1) + A(1)))
A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1)
3 + 3 + 3 + 3 + 3 + 3 + 3 + 3
24
Tubs




PostPosted: Tue Jan 24, 2006 10:25 pm   Post subject: (No subject)

Thanks a lot wtd. You rock.
wtd




PostPosted: Tue Jan 24, 2006 10:52 pm   Post subject: (No subject)

Glad I could help.

code:
(defun a (&optional (n 1))
  (cond ((= n 1) 3)
        ((> n 1)
         (let ((v (a (/ n 2))))
           (+ v v)))
        (t
         (let ((v (a (* n 2))))
           (+ v v)))))


Wink
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 4  [ 52 Posts ]
Goto page 1, 2, 3, 4  Next
Jump to:   


Style:  
Search: