logo
down
shadow

Structer pointer and pointer char array malloc array


Structer pointer and pointer char array malloc array

By : Russell Moore
Date : December 05 2020, 12:23 PM
I think the issue was by ths following , Your pointer inside createSchool has local scope, so global pointer is not modified. Faster fix is to return new allocated memory back to caller.
code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct School{
    char *school_name;
    int student_size;
}*high_school;

struct School* createSchool(struct School *s, char *schl_name, int student, int school_size)
{

    if(school_size == 1)
    {
        s = malloc(sizeof(struct School));
    }
    else
    {
        s = realloc(s, (school_size*sizeof(struct School)));
    }

    if (s != NULL)
    {
        s[school_size-1].student_size = student;
        s[school_size-1].school_name = malloc(strlen(schl_name)+1);

        strcpy(s[school_size-1].school_name, schl_name);

        for(int i=0; i<school_size; i++)
        {
            printf("%s\t%d\n", s[i].school_name, s[i].student_size);
        }
        printf("\n\n");
    }

    return s;
}

int main(void)
{
    int i = 1;
    high_school = createSchool(high_school, "Harvard", 50, i);
    i++;
    high_school = createSchool(high_school, "Oxford", 40, i);
    i++;
    high_school = createSchool(high_school, "MIT", 30, i);
}
struct School* createSchool(struct School *s, char *schl_name, int student, int school_size)
{

    if(school_size == 1){
        s = malloc(sizeof(struct School));
    }
    else
    {
        struct School *temp = realloc(s, (school_size*sizeof(struct School)));

        if (temp == NULL)
            return s;

        s = temp;    
    }

    if (s != NULL)
    {
        s[school_size-1].student_size = student;
        s[school_size-1].school_name = malloc(strlen(schl_name)+1);

        strcpy(s[school_size-1].school_name, schl_name);

        for(int i=0; i<school_size; i++)
        {
            printf("%s\t%d\n", s[i].school_name, s[i].student_size);
        }
        printf("\n\n");
    }

    return s;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct School{
    char *school_name;
    int student_size;
}*high_school;

void createSchool(struct School **s, char *schl_name, int student, int school_size)
{

    if(school_size == 1)
    {
        *s = malloc(sizeof(struct School));
    }
    else
    {
        struct School *temp = realloc(*s, (school_size*sizeof(struct School)));

        if (temp == NULL)
            return;

        *s = temp;
    }

    if (*s != NULL)
    {
        (*s)[school_size-1].student_size = student;
        (*s)[school_size-1].school_name = malloc(strlen(schl_name)+1);

        strcpy((*s)[school_size-1].school_name, schl_name);

        for(int i=0; i<school_size; i++)
        {
            printf("%s\t%d\n", (*s)[i].school_name, (*s)[i].student_size);
        }
        printf("\n\n");
    }
}

int main(void)
{
    int i = 1;
    createSchool(&high_school, "Harvard", 50, i);
    i++;
    createSchool(&high_school, "Oxford", 40, i);
    i++;
    createSchool(&high_school, "MIT", 30, i);
}
    (*s)[school_size-1].school_name = schl_name;


Share : facebook icon twitter icon
malloc char array pointer in c gives error

malloc char array pointer in c gives error


By : Pierre Prost
Date : March 29 2020, 07:55 AM
it fixes the issue , cHighValue is a pointer to a char array.
Allocate as
code :
cHighValue=malloc(sizeof(char)*20*X);
Working with malloc, char array and pointer

Working with malloc, char array and pointer


By : user3696955
Date : March 29 2020, 07:55 AM
help you fix your problem
Why is the size of the charBuffer 8(see first line of output) although I have allocated a size of 10?
segmentation fault in c about using malloc, and char array pointer

segmentation fault in c about using malloc, and char array pointer


By : Mehul Bhanushali
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further To read in an arbitrary long input string, you must use some kind of memory re-allocation when the input string grows beyond the already allocated memory. For instance you could use realloc like this:
code :
#include <stdio.h>
#include <stdlib.h>

#define INCREASE 32

int main(void) {
    int c;
    int allocated = INCREASE;
    int used = 1;
    char* in = malloc(allocated*sizeof(char));
    if (!in) exit(1);
    *in = '\0';

    while((c = fgetc(stdin)) != EOF && c != '\n')
    {
        if (used > (allocated-1))
        {
            // printf("Realloc\n");
            allocated += INCREASE;
            char* t = realloc(in, allocated);
            if (t)
            {
                in = t;
            }
            else
            {
                free(in);
                exit(1);
            }
        }

        in[used-1] = c;
        in[used] = '\0';
        ++used;
    }
    printf("%s\n", in);
    free(in);
    return 0;
}
How to assign memory using shared pointer instead of malloc for char array

How to assign memory using shared pointer instead of malloc for char array


By : TefaSmile
Date : March 29 2020, 07:55 AM
Hope that helps I have a character array and I want to initialize it using shared pointer I wrote like this below, but getting syntax error. Can anyone suggest what needs to be modified to make this work. I want the size of the array based on the rows and column. The maparray will hold bunch characters of '*'. Something like: , You can use the same by specifying the Array delete as
code :
shared_ptr<unsigned char> sh (new unsigned char[10], std::default_delete<unsigned char[]>());
unique_ptr<unsigned char[]> sh = make_unique<unsigned char[]>(10);
How do I convert a char * string into a pointer to pointer array and assign pointer values to each index?

How do I convert a char * string into a pointer to pointer array and assign pointer values to each index?


By : Daham Ilac
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a char * that is a long string and I want to create a pointer to a pointer(or pointer array). The char ** is set with the correct memory allocated and I'm trying to parse each word from the from the original string into a char * and place it in the char **. , Assuming that you know the number of words, it is trivial:
code :
char **newtext = malloc(3 * sizeof(char *));   // allocation for 3 char *
// Don't: char * pointing to non modifiable string litterals
// char * t1 = "fus", t2 = "roh", t3 = "dah";
char t1[] = "fus", t2[] = "roh", t3[] = "dah"; // create non const arrays

/* Alternatively
char text[] = "fus roh dah";    // ok non const char array
char *t1, *t2, *t3;
t1 = text;
text[3] = '\0';
t2 = text + 4;
texts[7] = '\0';
t3 = text[8];
*/
newtext[0] = t1;
newtext[1] = t2;
newtext[2] = t2;
Related Posts Related Posts :
  • perlbench results in segfault outside the SPEC 2006 harness
  • Terminal resize in chained ncurses programs (fork/exec/wait)
  • Comparison of one element in array to all other elements in same array
  • In C, can we read from pipes without busywaiting, may be using callbacks or other ways?
  • Check current time (without date) is between two times in C
  • I have something like this code and I would like to know how to get the number of elements of the array I pass in, if th
  • Difference between debug and release executable, in example code, in terms of symbols and functions optimised
  • find the unmatched elements at each index in two arrays
  • Structure Output are Different
  • Linux read() call not returning error when i Unplug serial cable
  • fread and ferror don't set errno
  • ATmega32U4: enabling interrupts hangs
  • Printing 2d array box
  • C - Dynamic Memory Allocation
  • How to read a string from a C function into Go?
  • Application is getting killed without any reason. Suspecting high BSS. How to debug it?
  • Dividing with/without using floats in C
  • Preprocessor conditional distributed across `#include` files
  • Typecasting a void pointer in a Linked List
  • Basic Functions in C
  • A for loop that calculates the even or odd then adds them up
  • Pascal String to cstring
  • Assigning to two pointers at the same time in C
  • getchar returns a different character
  • Understanding the enhanced syntax of for loop
  • Comparing char to string - C
  • How to omit the Bios Parameter Block in a bootloader?
  • Search linked list by substring and create a new linkedlist with all structs that contain the substring
  • Procedure and pointers C
  • RegOpenCurrentUser(KEY_WRITE) on newly created user
  • C Program with Pointer Increment False Values
  • Why won't a chunck of this program run in Linux but will on Windows?
  • Flow of the fork, how many forks do I have?
  • C recursive function for product receiving segmentation fault
  • C String concatenation using memcpy not appending
  • Properly Partitioning QuickSort Array
  • Place String Literal Before Variable
  • Sending payload with sigaction
  • using sizeof(void) inside malloc
  • Getting unused variable warning
  • C - Writing on second line of file
  • Unable to copy ip address from string to sockaddr_in
  • Write a in c program using loop control to produce the following output
  • GCC not warning on implicit cast from void * to void **
  • char[] vs char* in scanf C when writing memory
  • Recursive Calculation of a queue
  • How to rotate bits to the left and add rotated bits to the right
  • Print a size_t in a OS-independent architecture independent way
  • Radix Sort for floats in C - negative values get corrupted
  • ANSI C Separating the data from the file
  • Pascal - how sets work?
  • K&R 1.6 Sample Code Displays Long Integers
  • Unexpected Infinite for loop
  • using struct value in realloc gives segmentation fault
  • c - Error: "incomplete type is not allowed" , IAR compiler
  • How use correctly the pointers in a multi dimensional array?
  • Optimize hollow diamond pattern printing in c with minimum loops
  • Store an string on a Shared Memory C
  • How to create a sine function in c
  • Initializing n integers in 2-dimensional array
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com