Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

/*C - program to declare int, char and float variables dynamically.*/
#include<stdio.h>
#include<stdlib.h>

int main(){
//declare pointer variables
int *i;
char *c;
float *f;

//initialize size to pointers
i=(int*) malloc(sizeof(int));
c=(char*) malloc(sizeof(char));
f=(float*)malloc(sizeof(float));

//assign values
*i=100;
*c='N';
*f=123.45f;

//print values
printf("value of i= %d\n",*i);
printf("value of c= %c\n",*c);
printf("value of f= %f\n",*f);

free(i);
free(c);
free(f);

return 0;
}


I could see the above code in that char is defined dynamically but it will hold only one byte since it is declared as c=(char*) malloc(sizeof(char));

- Srinivas September 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *str1 = "I am working in Client Environment";
char *str2 = 0x00;

str2 = (char *)malloc( strlen( str1 ) + 1 ); /* the +1 is for the null terminator */
Remember sizeof returns the size of the data type. A pointer is 4 bytes. strlen gets the length of the string the pointer points to.

Again, you would free() str2 when you're done with it. Also check to see if the pointer you malloc'd or strdup'd is null, which means the allocation failed.

- Srinivas October 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use strdup.

char *ptr = 0x00;
ptr = strdup( "some string" );
free( ptr );
strdup gets the length of the incoming string (including the null terminator), creates memory for it, and returns a pointer to the memory.

As with any memory allocation, make sure you free() it when you're done with it.

- Srinivas October 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

uppose I just need to read a string from the user and need to store it for the above case.If your user's input is going into a 'char *ptr', then you would use
char *newPtr = strdup( ptr );You can use gets( ), scanf( ), etc. for reading input from the console.

- Srinivas October 03, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More