Question

Could someone please help me with this C language code I'm confused as to why i'm...

Could someone please help me with this C language code I'm confused as to why i'm getting an error every time I run it on command line if some could please help me as soon as possible.

#include <stdio.h>

#include <stdlib.h>

int main()

{

char *ptr_two = (char *)malloc(sizeof(char)*50);

printf("%p\n", ptr_two);

ptr_two = "A constant string in C";

printf("%p\n", ptr_two);

printf("%s\n", ptr_two);

free(ptr_two);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>

int main() {
    char *ptr_two = (char *) malloc(sizeof(char) * 50);
    printf("%p\n", ptr_two);
    free(ptr_two);  // free memory here, since we are going to assign a string to it on next line.
    ptr_two = "A constant string in C";
    printf("%p\n", ptr_two);
    printf("%s\n", ptr_two);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Could someone please help me with this C language code I'm confused as to why i'm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with this question (in C programming language and Unix) computer system book 3 edi...

    please help me with this question (in C programming language and Unix) computer system book 3 edition int *fun (int *x)f int value *xtt return &value; int main) int pl; int **p2; int a; char str [5] printf ("Enter a number:") scanf("%d", a) ; printf ("Enter a line:") gets (str); p1-malloc(sizeof (int)) *pl-p2; p2-malloc(sizeof (int)); pl-fun (a); n-0; for (pl-stripl<-str+5;pl++) if (isdigit (pl)) at+; return 0; Construct a table that shows for each line of code, what may be the...

  • I'm having trouble getting my program to output What is your first name? damion what is...

    I'm having trouble getting my program to output What is your first name? damion what is your last name? anderson damion, Your first name is 6 characters Your last name, anderson, is 8 characters Your name in reverse is: noimad nosredna This is my code so far: #include <stdlib.h> #include <stdio.h> void sizeOfName(char** name); void printSizeOfName(int *first, int *last, char** name); void reverseString(int *first, int *last, char** name); int main() {   char** name;   name = (char**)malloc(2*sizeof(char*));   name[0] = (char*)malloc(100*sizeof(char));   name[1]...

  • What does this code do? On the flipside of this paper, write the same code in...

    What does this code do? On the flipside of this paper, write the same code in Java. #include <stdio.h> #include <stdlib.h> int main ) ( char *str: (char *) malloc (15) ; str strcpy (str, "Iron"); printf("String 8u\n", str, str); Address %s, (char ) realloc (str, 25); str strcat (str, "Maiden"); Su\n", str, str): Address %s, printf ("String free (str) return (0); What does this code do? On the flipside of this paper, write the same code in Java. #include...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • Need help adjusting this code in C. I'm not sure this one part of the code...

    Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { unsigned int address; unsigned int pg_num; unsigned int offset; unsigned int i; unsigned int n = 1000000; double time_taken;...

  • In the following code, what is the first line that introduces a memory leak into the...

    In the following code, what is the first line that introduces a memory leak into the program? (Type the line number into the box below) 1: #include <stdio.h> 2: #include <stdlib.h> 3: #include <string.h> 4: int main() { 5:            char *word1 = NULL; 6:            word1 = malloc(sizeof(char) * 11); 7:            word1 = "bramble"; 8:            char *word2 = NULL: 9:            word2 = malloc(sizeof(char) * 11); 10:          word2 = word1; 11:          return 0; 12: }

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT