You are not logged in.

#1 2012-10-26 16:18:23

Archdove
Member
Registered: 2011-09-23
Posts: 118

[SOLVED] c assign from tmp varialbe to pointer

hey

I'm looking for a quick way to assing the value from a char array to various pointers. The value of this tmp variable changes. Obviously all my variables will have the same value at the end. (should be different but ... pointers... )

char tmp[256];
scanf ("%s, &tmp);
adr->firstname = tmp;

scanf ("%s, &tmp);
adr->name = tmp;

The way i will do it if i don't find a better way is to malloc firstname to strlen of tmp and assign the new value. But shouldn't this be easier to do? Same goes for name and others.

How would you do it?

Last edited by Archdove (2012-10-26 20:51:43)

Offline

#2 2012-10-26 16:28:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] c assign from tmp varialbe to pointer

There are several problems there.  I'd guess the missing closing quotes are just typos.  But the tmp variable should not be passed by reference to scanf.  'tmp' is a pointer to the 256-byte data block, so as written you are passing a pointer to a pointer which will seg fault.

(m/c)alloc and strncpy would be my recommendation for copying the tmp string to the adr members - that is if the adr member variables are not expected to be modified during the course of the program.

Last edited by Trilby (2012-10-26 16:29:20)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2012-10-26 18:10:59

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] c assign from tmp varialbe to pointer

Archdove wrote:

I'm looking for a quick way to assing the value from a char array to various pointers. The value of this tmp variable changes. ...


I assume you meant "I am looking for a quick way to assign the value of elements in a char array to allocated space which is pointed to by various pointers."
Either that, or "I am looking for a quick way to take the address of a char array and cause various pointers to point at that address"

In the former, you will need to copy the characters from the source to the destination.
In the latter, you change the pointer to the address of tmp -- but:  Make certain tmp does not go out of scope.  Also whatch out for side effects.  If the contents of tmp change, then that which is pointed to by your pointer will have changed as well.

How would you do it?

Use malloc.

Last edited by ewaller (2012-10-26 18:12:02)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2012-10-26 20:51:29

Archdove
Member
Registered: 2011-09-23
Posts: 118

Re: [SOLVED] c assign from tmp varialbe to pointer

Since english is not my mother thoung i hope you will understand that expressing exactly what i want to is not that easy. Thanks for your input anyway.

Here's my solution:

void ui_input_address(address_t** adrOut){
  *adrOut = malloc(sizeof(address_t));
  address_t* adr = *adrOut;
  char tmp[256];
  int i_tmp;

  printf ("Create new address. Please specify:\n");

  printf ("Firstname: ");
  scanf ("%s", tmp);
  adr->firstname = malloc (sizeof (char) * strlen (tmp));
  strncpy (adr->firstname, tmp, sizeof(adr->firstname));

  printf ("Name: ");
  scanf ("%s", tmp);
  adr->name = malloc (sizeof (char) * strlen (tmp));
  strncpy (adr->name, tmp, sizeof (adr->name));

  printf ("Street: ");
  scanf ("%s", tmp);
  adr->street = malloc (sizeof (char) * strlen (tmp));
  strncpy (adr->street, tmp, sizeof (adr->street));

  printf ("Street Nbr: ");
  scanf ("%s", tmp);
  adr->street_nbr = malloc (sizeof (char) * strlen (tmp));
  strncpy (adr->street_nbr, tmp, sizeof (adr->street_nbr));

  printf ("Zip: ");
  scanf ("%d", &i_tmp);
  adr->zip = i_tmp;

  printf ("City: ");
  scanf ("%s", tmp);
  adr->city = malloc (sizeof (char) * strlen (tmp));
  strncpy (adr->city, tmp, sizeof (adr->city));
}

Would be nicer to do this in a loop but it will suffice for now. Tanks guys.

Regards,
dove

Offline

#5 2012-10-26 22:43:31

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] c assign from tmp varialbe to pointer

One last little thing.....    Now that you have allocated memory, there comes a time in every program when those data are no longer needed.  Make sure you eventually release any memory you allocate.  Of course, ending a program will release that memory; but that is not how it supposed to be done.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2012-10-26 23:25:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] c assign from tmp varialbe to pointer

I anticipate the code provided will cause a number of problems when you try to access that data.  Strlen does not account for null terminating characters, so your newly allocated pointers only have enough space for the content of the string, not the termination.  Then you copy only the content.

If/when you try to use these newly allocated variables you will not be happy with the result.

The easy solution is to change every "strlen(tmp)" to "strlen(tmp)+1".

As an aside, this is why I generally use calloc when allocating string space - that way even if I screw up there should be plenty of "null" bytes to terminate whatever data is put into that memory location.  But malloc would be fine as long as you ensure you allocate the right amount of space and copy over - or add - the null characters yourself.

Last edited by Trilby (2012-10-26 23:30:59)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2012-10-26 23:56:44

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] c assign from tmp varialbe to pointer

Trilby wrote:

...As an aside, this is why I generally use calloc when allocating string space - that way even if I screw up there should be plenty of "null" bytes to terminate whatever data is put into that memory location.

You surprise me.  I have looked at your code and have watched your comments here for some time.  I know you are far too conscientious to rely on a mechanism like that.  Not that it may not be a good idea, I just bet you don't fall into that trap often.  (BTW, this was meant as a compliment smile )


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#8 2012-10-27 00:04:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] c assign from tmp varialbe to pointer

Thanks ... for the compliment part.

I see that use of calloc a bit like wearing a seatbelt while driving.  I really should never need it.  But it's a handly layer of protection if all else goes wrong.  I certainly don't "rely" on this, but it is a nice safety net to know things should never get to out of hand.

Though it only really applies when a larger-than-needed block of memory would be allocated anyway.

And, if I am competant at coding, it's only because I've gone through the steps of making every possible stupid mistake over and over.  I anticipate making many more in my future.  In a similar vane, my graduate advisor once asked me about a proposed experiment if I had accounted for any unanticipated problems.  I responded "of course not.  I have, however, tried hard to anticipate any outcome, and I have accounted for all of those."  One can never prepare for unanticipated problems without taking steps they think/hope should never be needed.

Last edited by Trilby (2012-10-27 00:08:44)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2012-10-27 01:21:25

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: [SOLVED] c assign from tmp varialbe to pointer

You may want to use strdup(3) instead of malloc(3)/strlen(3)/strncpy(3) is not ANSI but POSIX wink Also you may want to use fgets(3)/sscanf(3) when you are reading an integer otherwise surprises will be happens [#1] anyways I also recomends when reading strings wink

strdup(3) calls malloc(3), so this memory needs to be freed with free(3).

[#1] http://c-faq.com/stdio/scanfprobs.html

Last edited by djgera (2012-10-27 01:22:09)

Offline

#10 2012-10-27 21:01:56

Archdove
Member
Registered: 2011-09-23
Posts: 118

Re: [SOLVED] c assign from tmp varialbe to pointer

Thanks for all the comments. To free my memory i have prepared destroy functions. So no problems there. The thing with the string termination however is really handy advise.

I will look into this as soon as i find the time.

Offline

Board footer

Powered by FluxBB